1 Set Up

1.1 R Code

#packages we need for this code file
library(ggplot2)
library(mgcv)
library(lubridate)
library(zoo)
library(tidyverse)
library(dplyr)
library(DHARMa)
library(mgcViz)
library(extrafont)
library(arm)
loadfonts()
library(stargazer)
library(ellipse)
library(dotwhisker)
library(countreg)
#define functions we will need for analysis
#expit function
expit<-function(x){
  return(exp(x)/(1 + exp(x)))
}

#logit function
logit<-function(x){
  return(log(x/(1 - x)))
}

1.2 Data

#read in data
main_analysis_data<-read.csv("./Data/full_data_set_11_29_21_unintentional.csv")

################################## set up data set ################################
#add the intervention dates and time period data
main_analysis_data$Intervention_First_Date<-as.Date(main_analysis_data$Intervention_First_Date)
main_analysis_data$Time_Period_Start<-as.Date(main_analysis_data$Time_Period_Start)
names(main_analysis_data)[which(colnames(main_analysis_data) == "sum_deaths")] <- "imputed_deaths"

################################## set up the Regions ##############################
#set up the regions according to Census: https://www.census.gov/geographies/reference-maps/2010/geo/2010-census-regions-and-divisions-of-the-united-states.html
NE.name <- c("Connecticut","Maine","Massachusetts","New Hampshire",
             "Rhode Island","Vermont","New Jersey","New York",
             "Pennsylvania")

MW.name <- c("Indiana","Illinois","Michigan","Ohio","Wisconsin",
             "Iowa","Kansas","Minnesota","Missouri","Nebraska",
             "North Dakota","South Dakota")

S.name <- c("Delaware","District of Columbia","Florida","Georgia",
            "Maryland","North Carolina","South Carolina","Virginia",
            "West Virginia","Alabama","Kentucky","Mississippi",
            "Tennessee","Arkansas","Louisiana","Oklahoma","Texas")

W.name <- c("Arizona","Colorado","Idaho","New Mexico","Montana",
            "Utah","Nevada","Wyoming","Alaska","California",
            "Hawaii","Oregon","Washington")

region.list <- list(
  Northeast=NE.name,
  Midwest=MW.name,
  South=S.name,
  West=W.name)

#initialize vector with "West" and then impute the other regions for the states
main_analysis_data$Region<-rep("West", nrow(main_analysis_data))
for(state in unique(main_analysis_data$State)){
  if(state %in% region.list$Northeast){
    main_analysis_data$Region[main_analysis_data$State == state]<-"Northeast"
  }else if(state %in% region.list$Midwest){
    main_analysis_data$Region[main_analysis_data$State == state]<-"Midwest"
  }else if(state %in% region.list$South){
    main_analysis_data$Region[main_analysis_data$State == state]<-"South"
  }
}

2 Sandwich Estimator Code

#here, we estimate the variance-covariance matrix through the sandwich estimator
#we create a function so that we don't have to keep writing the code:
#cov_data is such that rows are state-time combinations and columns are the different policy measures
#coef_values need to be in order of the columns of cov_data
#z_value is the z-value that corresponds to the CI. We default to 95% CI so we default to 1.96
#we take p as the number of parametesr for a bias correction

compute_sd_and_CI <- function(cov_data, observed_y, coef_values, z_value = 1.96, p,
                              print_full_cov = FALSE){
  middle_term <- matrix(0, nrow = ncol(cov_data), ncol = ncol(cov_data))
  for(i in 1:nrow(cov_data)){
    #sum_{s,t} (z_{s,t}z_{s,t}^T)*(y_{s,t}-z_{s,t}^T theta)^2
    middle_term <- middle_term + tcrossprod(as.matrix(cov_data[i,]))*
      as.numeric((observed_y[i] - t(as.matrix(cov_data[i,]))%*%coef_values)^2)
  }
  #(Z^T Z)^{-1}*middle_term*(Z^T Z)^{-1}
  var_cov <- solve(crossprod(cov_data))%*%(middle_term)%*%solve(crossprod(cov_data))*(nrow(cov_data)/(nrow(cov_data) - p))
  #we obtain the standard deviations by taking the square root of the diagonal of the variance-covariance matrix.
  sd_of_coefficients <- sqrt(diag(var_cov))
  
  #find the CI for the coefficients
  lb_coef <- coef_values - z_value*(sd_of_coefficients)
  ub_coef <- coef_values + z_value*(sd_of_coefficients)
  
  return_data_set <- data.frame(lb_coef, coef_values, ub_coef, sd_coef = sd_of_coefficients)
  
  if(print_full_cov){
    return(list(return_data_set = return_data_set, var_cov = var_cov))
  }else{
    return(return_data_set)
  }
}

3 Attributable Deaths Computation

3.1 Not Bootstrap

# attr_death_compute <- function(data, coef_data){
#   attr_table <- data.frame(matrix(NA, nrow = unique(data$Time_Period_ID), ncol = 4)) 
#   #filter data so that it's only states where there was treatment
#   data <- data %>%
#     filter(Intervention_Redefined > 0)
#   for(time in unique(data$Time_Period_ID)){
#     #filter data to time period t
#     time_data <- data %>%
#       filter(Time_Period_ID == time)
#     
#     #obtain the population
#     pop <- time_data$population
#     
#     #obtain the estimated probability of saved deaths: probability_OD^{(1)}*(1 - exp(-beta*x))
#     est_saved_death_prob <- time_data$prop_dead*
#       (exp(-(time_data$Intervention_Redefined*coef_data["Intervention_Redefined", "estimate"] +
#                time_data$lag_num_pd_w_tx*coef_data["lag_num_pd_w_tx", "estimate"])) - 1)
#     #estimate the number of OD had intervention not occurred by multiplying by the population
#     est_saved_death <- pop*est_saved_death_prob
#     
#     #obtain LB -- do the same thing as above but with the LB of the coefficient
#     est_prob_saved_death_lb <- time_data$prop_dead*
#       (exp(-(time_data$Intervention_Redefined*coef_data["Intervention_Redefined", "conf.low"] +
#                    time_data$lag_num_pd_w_tx*coef_data["lag_num_pd_w_tx", "conf.low"])) - 1)
#     
#     n_od_saved_lb <- pop*est_prob_saved_death_lb
#     
#     #obtain UB -- do the same thing as above but with the UB of the coefficient
#      est_prob_saved_death_ub <- time_data$prop_dead*
#        (exp(-(time_data$Intervention_Redefined*coef_data["Intervention_Redefined", "conf.high"] +
#                     time_data$lag_num_pd_w_tx*coef_data["lag_num_pd_w_tx", "conf.high"])) - 1)
#     
#     n_od_saved_ub <- pop*est_prob_saved_death_ub
# 
#     #save the results
#     attr_table[time,] <- c(time, sum(est_saved_death), 
#                         sum(n_od_saved_lb), 
#                         sum(n_od_saved_ub))
#     
#     
#   }
#   colnames(attr_table) <- c("Time_Period", "attr_deaths", "attr_deaths_lb", "attr_deaths_ub")
#   attr_table
# }
attr_death_compute <- function(data, coef_data, lin_model = FALSE, tx_name = NULL){
  attr_table <- data.frame(matrix(NA, nrow = unique(data$Time_Period_ID), ncol = 4)) 
  #filter data so that it's only states where there was treatment
  data <- data %>%
    filter(Intervention_Redefined > 0)
  for(time in unique(data$Time_Period_ID)){
    
    #filter data to time period t
    time_data <- data %>%
      filter(Time_Period_ID == time)
    
    #obtain the population
    pop <- time_data$population
    
    #obtain the estimated probability had intervention not occurred
    #here, we compute x^T*beta where x is a vector of the covariates and beta is the corresponding coefficients
    if(lin_model == FALSE){
      pd_coef_names <- sapply(0:39, function(k){paste("pos_", k, "_pd", sep = "")})
      est_prob_no_int <- time_data$prop_dead*exp(- as.matrix(time_data[,pd_coef_names])%*%
          as.matrix(coef_data[pd_coef_names, "estimate"]))
    }else{
    
      est_prob_no_int <- time_data$prop_dead*exp(- as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "estimate"]))
    }
    
    #estimated number of OD had intervention not occurred
    n_od_no_int <- pop*est_prob_no_int
    
    #obtain LB
    if(lin_model == FALSE){
      pd_coef_names <- sapply(0:39, function(k){paste("pos_", k, "_pd", sep = "")})
      est_prob_no_int_lb <- time_data$prop_dead*exp(- as.matrix(time_data[,pd_coef_names])%*%
          as.matrix(coef_data[pd_coef_names, "conf.low"]))
    }else{
      est_prob_no_int_lb <- time_data$prop_dead*exp(- as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "conf.low"]))
    }
    n_od_no_int_lb <- pop*est_prob_no_int_lb
    
    #obtain UB
    if(lin_model == FALSE){
      pd_coef_names <- sapply(0:39, function(k){paste("pos_", k, "_pd", sep = "")})
      est_prob_no_int_ub <- time_data$prop_dead*exp(- as.matrix(time_data[,pd_coef_names])%*%
          as.matrix(coef_data[pd_coef_names, "conf.high"]))
    }else{
      est_prob_no_int_ub <- time_data$prop_dead*exp(- as.matrix(time_data[,tx_name])%*%as.matrix(coef_data[tx_name, "conf.high"]))
    }
    n_od_no_int_ub <- pop*est_prob_no_int_ub

    
    attr_table[time,] <- c(time, sum(n_od_no_int) - sum(time_data$imputed_deaths), 
                        sum(n_od_no_int_lb)  - sum(time_data$imputed_deaths), 
                        sum(n_od_no_int_ub) - sum(time_data$imputed_deaths))
    
    
  }
  colnames(attr_table) <- c("Time_Period", "attr_deaths", "attr_deaths_lb", "attr_deaths_ub")
  attr_table
}

4 Bootstrap Data Code

#bootstrap code to estimate coefficients and attributable deaths
boostrap_state_time_group <- function(data, model, coef_of_interest, nSim = 1000, seed = 1234){
  coef_store_data <- data.frame(matrix(NA, nrow = nSim, ncol = length(coef_of_interest)))
  set.seed(seed)
  for(sim in 1:nSim){
    #sample the data so that it's the same size
    # sample_data <- sample(1:nrow(data), nrow(data), replace = TRUE)
    sample_data <- sample(unique(data$State), length(unique(data$State)), replace = TRUE)
    # boot_data <- data[sample_data,]
    boot_data <- data.frame()
    for(state in sample_data){
        boot_data <- rbind(boot_data, data[data$State == state,])
    }
    
    #fit model with bootstrao data
    boot_model <- update(model, data = boot_data)
    
    #store coefficient results 
    coef_store_data[sim,] <- coef(boot_model)[coef_of_interest]

  }
  return(coef_store_data)
}

boostrap_state_time_unit <- function(data, model, coef_of_interest, nSim = 1000, seed = 1234){
  coef_store_data <- data.frame(matrix(NA, nrow = nSim, ncol = length(coef_of_interest)))
  set.seed(seed)
  for(sim in 1:nSim){
    #sample the data so that it's the same size
    sample_data <- sample(1:nrow(data), nrow(data), replace = TRUE)
    # sample_data <- sample(unique(data$State), length(unique(data$State)), replace = TRUE)
    boot_data <- data[sample_data,]
    # boot_data <- data.frame()
    # for(state in sample_data){
    #     boot_data <- rbind(boot_data, data[data$State == state,])
    # 
    # }
    
    #fit model with bootstrao data
    boot_model <- update(model, data = boot_data)
    
    #store coefficient results 
    coef_store_data[sim,] <- coef(boot_model)[coef_of_interest]

  }
  return(coef_store_data)
}

5 Event Study Data Creation

main_analysis_data$prop_dead <- main_analysis_data$imputed_deaths/main_analysis_data$population
#create the dataset for the event study to check for pre-trend analysis
time_data_int <- main_analysis_data %>%
  #group by the state
  group_by(State) %>%
  #find the time interval ID for the intervention time
  summarise(intervention_time_id = ifelse(floor_date(Intervention_First_Date, "6 months") == Time_Period_Start, Time_Period_ID, NA)) %>%
  #filter out the other time periods that aren't the intervention date
  filter(!is.na(intervention_time_id))

#merge the time_data_int with the main dataset
merged_main_time_data_int <- merge(main_analysis_data, time_data_int, by = "State", all.x = TRUE)

#create the columns that associate with the periods before the intervention
#the max number of periods before the intervention is determined by the maximum time period of the intervention
neg_periods_df <- sapply(1:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 1),
#the indicator for x periods before intervention is equal to 1 if the time ID of intervention minus time ID is equal to x
                         function(x){ifelse(merged_main_time_data_int$intervention_time_id - 
                                              merged_main_time_data_int$Time_Period_ID == x, 1, 0)})

#create the column names
colnames(neg_periods_df) <- sapply(1:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 1), 
                                   function(x){paste("neg_", x, "_pd", sep = "")})
#add in the state and time ID columns
neg_periods_df <- cbind(neg_periods_df, "State" = merged_main_time_data_int$State, 
                        "Time_Period_ID" = merged_main_time_data_int$Time_Period_ID)
#for Hawaii, impute a 0 because it is NA right now
neg_periods_df[neg_periods_df[,"State"] == "Hawaii", 1:34] <- 0

#create the columns that associate with the periods after the intervention
#the max number of periods after the intervention is determined by the maximum Time ID minus the minus time period of the intervention
#the period 0 is associated with intervention time
pos_periods_df <- sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                         function(x){ifelse(merged_main_time_data_int$Time_Period_ID - 
                                              merged_main_time_data_int$intervention_time_id == x, 1, 0)})
#create the column names
colnames(pos_periods_df) <- sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")})
#add in the state and time ID columns
pos_periods_df <- cbind(pos_periods_df, "State" = merged_main_time_data_int$State, 
                        "Time_Period_ID" = merged_main_time_data_int$Time_Period_ID)
#for Hawaii, impute a 0 because it is NA right now
pos_periods_df[pos_periods_df[,"State"] == "Hawaii", 1:40] <- 0

#merge the columns of indicators for before and after the intervention with the main analysis data to create the dataset for event study
sensitivity_anlys_event_study_data <- merge(main_analysis_data, 
                                            neg_periods_df, by = c("State", "Time_Period_ID"))

sensitivity_anlys_event_study_data <- merge(sensitivity_anlys_event_study_data, 
                                            pos_periods_df, by = c("State", "Time_Period_ID"))
#change the indicator values to numeric type 
neg_1_index <- which(colnames(sensitivity_anlys_event_study_data) == "neg_1_pd")
pos_39_index <- which(colnames(sensitivity_anlys_event_study_data) == "pos_39_pd")

sensitivity_anlys_event_study_data[, neg_1_index:pos_39_index] <- apply(sensitivity_anlys_event_study_data[, neg_1_index:pos_39_index], 
                                                                      2, as.numeric)

6 OLS Model Main Analysis With Smoothed Time Effects With Log Proportion

#compute the proportion of people who died from drug overdose
main_analysis_data$prop_dead <- main_analysis_data$imputed_deaths/main_analysis_data$population

#fit an OLS with smoothed time effects
main_analysis_model_log_smoothed_time<-gam(log(prop_dead)~ State +
                           s(Time_Period_ID, bs = "cr", by = as.factor(Region)) +
                           Naloxone_Pharmacy_Yes_Redefined +
                           Naloxone_Pharmacy_No_Redefined +
                           Medical_Marijuana_Redefined +
                           Recreational_Marijuana_Redefined +
                           GSL_Redefined +
                           PDMP_Redefined +
                           Medicaid_Expansion_Redefined +
                           Intervention_Redefined ,
                         data = main_analysis_data)

summary(main_analysis_model_log_smoothed_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + Naloxone_Pharmacy_No_Redefined + 
##     Medical_Marijuana_Redefined + Recreational_Marijuana_Redefined + 
##     GSL_Redefined + PDMP_Redefined + Medicaid_Expansion_Redefined + 
##     Intervention_Redefined
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.711293   0.052777 -184.006  < 2e-16 ***
## StateAlaska                       0.164106   0.074380    2.206 0.027480 *  
## StateArizona                      0.288302   0.067744    4.256 2.18e-05 ***
## StateArkansas                    -0.487283   0.066740   -7.301 4.16e-13 ***
## StateCalifornia                  -0.155692   0.074371   -2.093 0.036440 *  
## StateColorado                     0.035981   0.074125    0.485 0.627444    
## StateConnecticut                  0.230076   0.070893    3.245 0.001193 ** 
## StateDelaware                     0.216431   0.067992    3.183 0.001480 ** 
## StateFlorida                      0.309018   0.066797    4.626 3.97e-06 ***
## StateGeorgia                      0.000433   0.066781    0.006 0.994827    
## StateHawaii                      -0.378239   0.072961   -5.184 2.40e-07 ***
## StateIdaho                       -0.124420   0.066788   -1.863 0.062628 .  
## StateIllinois                     0.161001   0.067802    2.375 0.017667 *  
## StateIndiana                      0.081619   0.066357    1.230 0.218845    
## StateIowa                        -0.676997   0.066568  -10.170  < 2e-16 ***
## StateKansas                      -0.230527   0.066060   -3.490 0.000495 ***
## StateKentucky                     0.702316   0.066783   10.516  < 2e-16 ***
## StateLouisiana                    0.340740   0.065973    5.165 2.66e-07 ***
## StateMaine                        0.088824   0.073874    1.202 0.229369    
## StateMaryland                    -1.513084   0.067786  -22.321  < 2e-16 ***
## StateMassachusetts               -0.083599   0.067340   -1.241 0.214593    
## StateMichigan                     0.010429   0.068383    0.153 0.878801    
## StateMinnesota                   -0.643068   0.069892   -9.201  < 2e-16 ***
## StateMississippi                 -0.049288   0.066014   -0.747 0.455380    
## StateMissouri                     0.167659   0.068266    2.456 0.014138 *  
## StateMontana                     -0.439514   0.070271   -6.255 4.90e-10 ***
## StateNebraska                    -0.874032   0.067084  -13.029  < 2e-16 ***
## StateNevada                       0.460178   0.071746    6.414 1.78e-10 ***
## StateNew Hampshire                0.147406   0.067147    2.195 0.028264 *  
## StateNew Jersey                   0.080613   0.067947    1.186 0.235611    
## StateNew Mexico                   0.645396   0.072906    8.852  < 2e-16 ***
## StateNew York                    -0.139492   0.068359   -2.041 0.041429 *  
## StateNorth Carolina               0.233840   0.065853    3.551 0.000393 ***
## StateNorth Dakota                -1.137555   0.066451  -17.119  < 2e-16 ***
## StateOhio                         0.452817   0.066969    6.762 1.80e-11 ***
## StateOklahoma                     0.464010   0.066457    6.982 3.99e-12 ***
## StateOregon                      -0.271552   0.073846   -3.677 0.000242 ***
## StatePennsylvania                 0.561023   0.066850    8.392  < 2e-16 ***
## StateRhode Island                -0.278774   0.069373   -4.018 6.08e-05 ***
## StateSouth Carolina               0.207544   0.066391    3.126 0.001798 ** 
## StateSouth Dakota                -1.027424   0.066779  -15.386  < 2e-16 ***
## StateTennessee                    0.468655   0.065670    7.136 1.35e-12 ***
## StateTexas                       -0.019383   0.066729   -0.290 0.771481    
## StateUtah                        -0.095477   0.066061   -1.445 0.148542    
## StateVermont                     -0.169566   0.069677   -2.434 0.015041 *  
## StateVirginia                    -0.032103   0.065975   -0.487 0.626602    
## StateWashington                   0.045026   0.075228    0.599 0.549557    
## StateWest Virginia                0.790500   0.066804   11.833  < 2e-16 ***
## StateWisconsin                    0.006409   0.066116    0.097 0.922789    
## StateWyoming                     -0.021573   0.066041   -0.327 0.743956    
## Naloxone_Pharmacy_Yes_Redefined  -0.078516   0.042517   -1.847 0.064946 .  
## Naloxone_Pharmacy_No_Redefined   -0.001783   0.038500   -0.046 0.963069    
## Medical_Marijuana_Redefined       0.192033   0.030656    6.264 4.62e-10 ***
## Recreational_Marijuana_Redefined -0.110335   0.048796   -2.261 0.023863 *  
## GSL_Redefined                     0.054084   0.031598    1.712 0.087127 .  
## PDMP_Redefined                   -0.152943   0.024680   -6.197 7.02e-10 ***
## Medicaid_Expansion_Redefined      0.091989   0.030149    3.051 0.002311 ** 
## Intervention_Redefined           -0.026254   0.024341   -1.079 0.280903    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.779  5.836 140.15  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.464  8.917  82.99  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.300  7.442 105.57  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      3.355  4.185  86.33  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.84   Deviance explained = 84.6%
## GCV = 0.089598  Scale est. = 0.085974  n = 2000
gam.check(main_analysis_model_log_smoothed_time, page = 1)

## 
## Method: GCV   Optimizer: magic
## Smoothing parameter selection converged after 5 iterations.
## The RMS GCV score gradient at convergence was 1.134567e-06 .
## The Hessian was positive definite.
## Model rank =  94 / 94 
## 
## Basis dimension (k) checking results. Low p-value (k-index<1) may
## indicate that k is too low, especially if edf is close to k'.
## 
##                                                k'  edf k-index p-value
## s(Time_Period_ID):as.factor(Region)Midwest   9.00 4.78    1.05    1.00
## s(Time_Period_ID):as.factor(Region)Northeast 9.00 8.46    1.05    0.99
## s(Time_Period_ID):as.factor(Region)South     9.00 6.30    1.05    0.99
## s(Time_Period_ID):as.factor(Region)West      9.00 3.36    1.05    0.98
#examine fitted values
summary(fitted(main_analysis_model_log_smoothed_time))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -12.314 -10.207  -9.739  -9.796  -9.341  -8.122
hist(fitted(main_analysis_model_log_smoothed_time))

#smoothed effects
plot(main_analysis_model_log_smoothed_time, pages = 1)

6.1 Sandwich Estimator: Coefficients and 95% CI

#compute the full dataset including basis functions
full_df_w_basis_functions_log_smoothed_time <- as.matrix(data.frame(predict(main_analysis_model_log_smoothed_time, type = "lpmatrix")))

#estimate the 95% CI and SD
coefficient_values_log_smoothed_time <- coef(main_analysis_model_log_smoothed_time)
#type = "response" to get the estimated probabilities
main_analysis_sd_and_ci_log_smoothed_time <- compute_sd_and_CI(full_df_w_basis_functions_log_smoothed_time,
                                                               log(main_analysis_data$prop_dead),
                                                               coefficient_values_log_smoothed_time,
                                                               p = ncol(full_df_w_basis_functions_log_smoothed_time) - 50)
# format(round(main_analysis_sd_and_ci_log_smoothed_time, 3), nsmall = 3)

colnames(main_analysis_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high", "sd")
main_analysis_sd_and_ci_log_smoothed_time$term <- rownames(main_analysis_sd_and_ci_log_smoothed_time)


main_analysis_sd_and_ci_log_smoothed_time$ci_95 <- 
  paste("95% CI = (", format(round(main_analysis_sd_and_ci_log_smoothed_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(main_analysis_sd_and_ci_log_smoothed_time$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(main_analysis_sd_and_ci_log_smoothed_time[51:58,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Exposure Intervention") + 
  scale_color_grey()    + 
  geom_text(main_analysis_sd_and_ci_log_smoothed_time[51:58,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 8:1), size = 3) + 
  geom_text(main_analysis_sd_and_ci_log_smoothed_time[51:58,], 
            mapping = aes(label = ci_95, x = 0.9, y = 8:1), size = 3) +
  xlim(-.3, 1.1)

6.2 Attributable Deaths

date_data <- main_analysis_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_smoothed_time <- attr_death_compute(main_analysis_data, main_analysis_sd_and_ci_log_smoothed_time, 
                                                        lin_model = TRUE, tx_name = "Intervention_Redefined")
attr_deaths_est_log_smoothed_time <- merge(attr_deaths_est_log_smoothed_time, date_data, by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_smoothed_time, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.3 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_smoothed_eff_log_outcome <- boostrap_state_time(main_analysis_data, main_analysis_model_log_smoothed_time,
                                                          lin_model = TRUE, tx_name = "Intervention_Redefined")

reduced_coef <- Reduce("cbind", bootstrap_smoothed_eff_log_outcome[[1]])
apply(reduced_coef, 1, quantile, probs = c(.025, .975))
apply(reduced_coef, 1, mean)

coef_w_ci_smoothed_time_log_outcome <- cbind(t(apply(reduced_coef, 1, quantile, probs = c(.025, .975))), apply(reduced_coef, 1, mean))
colnames(coef_w_ci_smoothed_time_log_outcome) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_smoothed_time_log_outcome <- data.frame(coef_w_ci_smoothed_time_log_outcome)
coef_w_ci_smoothed_time_log_outcome$term <- rownames(coef_w_ci_smoothed_time_log_outcome)

dwplot(coef_w_ci_smoothed_time_log_outcome[51:58,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Single Intervention, Using Bootstrap") + 
  scale_color_grey()  
  # coord_flip() +
  # geom_hline(yintercept = 33, col = "red", linetype = "dashed")

list_attr_deaths <- lapply(bootstrap_smoothed_eff_log_outcome[[2]], function(x){x$attr_deaths})
reduced_attr_deaths <- Reduce("cbind", list_attr_deaths)
attr_deaths_smoothed_time_log_outcome <- cbind(t(apply(reduced_attr_deaths, 1, quantile, probs = c(.025, .975), na.rm = TRUE)), 
                                               apply(reduced_attr_deaths, 1, mean, na.rm = TRUE))
colnames(attr_deaths_smoothed_time_log_outcome) <- c("conf.low", "conf.high", "estimate")
attr_deaths_smoothed_time_log_outcome <- data.frame(attr_deaths_smoothed_time_log_outcome)
attr_deaths_smoothed_time_log_outcome$Time_Period_Start <- unique(main_analysis_data$Time_Period_Start)

ggplot(attr_deaths_smoothed_time_log_outcome, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.4 Event Study: All Periods

6.4.1 Model Fitting

#create a formula for the gam model which includes the state effects, smoothed time effects, policy measures, 
#the periods before the intervention (excluding 1 period and 34 periods before intervention)
#the intervention period, and the periods after the intervention

formula_event_study_log_smoothed_time <- formula(paste("log(prop_dead) ~ State +
                                           s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                     paste(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)-2), 
                                            function(x)paste("neg_", x, "_pd", sep = "")), collapse = "+"),
                                     "+",
                                     paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                              function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_event_study_model_log_smoothed_time<-gam(formula_event_study_log_smoothed_time,
                                         data = sensitivity_anlys_event_study_data)

# summary(sensitivity_anlys_event_study_model_log_smoothed_time)

6.4.2 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_event_study_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_event_study_log_smoothed_time <- coef(sensitivity_anlys_event_study_model_log_smoothed_time)
#type = "response" to get the estimated probabilities
sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time <-
  compute_sd_and_CI(as.matrix(full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time), 
                                                             log(sensitivity_anlys_event_study_data$prop_dead),
                                                             coefficient_values_sensitivity_anlys_event_study_log_smoothed_time,
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_event_study_log_smoothed_time) - 50)
(sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time)
##                                                     lb_coef  coef_values
## (Intercept)                                    -9.728823888 -9.607755377
## StateAlaska                                    -0.047405904  0.101715259
## StateArizona                                    0.146009989  0.257887425
## StateArkansas                                  -0.625065356 -0.516038962
## StateCalifornia                                -0.195801439 -0.062450464
## StateColorado                                  -0.120135673  0.002447472
## StateConnecticut                                0.089301661  0.222377470
## StateDelaware                                   0.020468319  0.155066167
## StateFlorida                                    0.294377706  0.412924559
## StateGeorgia                                    0.013906978  0.127609736
## StateHawaii                                    -0.608365093 -0.467150486
## StateIdaho                                     -0.275351996 -0.159066931
## StateIllinois                                   0.104812517  0.233581709
## StateIndiana                                   -0.023612076  0.076071921
## StateIowa                                      -0.732961003 -0.624312899
## StateKansas                                    -0.320955862 -0.224552743
## StateKentucky                                   0.592302264  0.686996193
## StateLouisiana                                  0.293469508  0.394888461
## StateMaine                                     -0.063869300  0.074857829
## StateMaryland                                  -1.672434093 -1.451851831
## StateMassachusetts                             -0.299079313 -0.083062578
## StateMichigan                                  -0.045169095  0.050576690
## StateMinnesota                                 -0.753441298 -0.645251331
## StateMississippi                               -0.209093790 -0.087268616
## StateMissouri                                   0.086401505  0.185702542
## StateMontana                                   -0.509216034 -0.394331541
## StateNebraska                                  -1.017176026 -0.912234589
## StateNevada                                     0.385989567  0.495672978
## StateNew Hampshire                              0.012195381  0.124591896
## StateNew Jersey                                -0.008620296  0.145727757
## StateNew Mexico                                 0.488774253  0.622210509
## StateNew York                                  -0.251050987 -0.132279799
## StateNorth Carolina                             0.187299971  0.274412387
## StateNorth Dakota                              -1.341397313 -1.172707883
## StateOhio                                       0.455678130  0.589151698
## StateOklahoma                                   0.337846240  0.453117480
## StateOregon                                    -0.415292220 -0.299059866
## StatePennsylvania                               0.561017187  0.677459526
## StateRhode Island                              -0.586170729 -0.312527565
## StateSouth Carolina                             0.046751608  0.161134027
## StateSouth Dakota                              -1.216535630 -1.084870928
## StateTennessee                                  0.383486326  0.470729691
## StateTexas                                     -0.032252917  0.087487525
## StateUtah                                      -0.238697614 -0.054041819
## StateVermont                                   -0.333329255 -0.191087648
## StateVirginia                                  -0.067956844  0.031405848
## StateWashington                                -0.095354561  0.024086450
## StateWest Virginia                              0.618242919  0.763862290
## StateWisconsin                                 -0.040024448  0.055350995
## StateWyoming                                   -0.156265025 -0.026862195
## Naloxone_Pharmacy_Yes_Redefined                -0.125945151 -0.061319716
## Naloxone_Pharmacy_No_Redefined                 -0.060956513  0.003373398
## Medical_Marijuana_Redefined                     0.129985765  0.199070518
## Recreational_Marijuana_Redefined               -0.174441233 -0.098626529
## GSL_Redefined                                   0.008441128  0.063283495
## PDMP_Redefined                                 -0.233796444 -0.178713621
## Medicaid_Expansion_Redefined                    0.038062580  0.089195898
## neg_2_pd                                       -0.090842524  0.020524738
## neg_3_pd                                       -0.093266935  0.016757110
## neg_4_pd                                       -0.130124958 -0.010783798
## neg_5_pd                                       -0.128729866 -0.013004948
## neg_6_pd                                       -0.115563431 -0.005778559
## neg_7_pd                                       -0.159146724 -0.044021509
## neg_8_pd                                       -0.243293082 -0.100544147
## neg_9_pd                                       -0.180033617 -0.025158944
## neg_10_pd                                      -0.117013914  0.024031356
## neg_11_pd                                      -0.123358860  0.025412131
## neg_12_pd                                      -0.047903816  0.115435529
## neg_13_pd                                      -0.150624459  0.015177292
## neg_14_pd                                      -0.172638404 -0.015729613
## neg_15_pd                                      -0.246143938 -0.059495317
## neg_16_pd                                      -0.215163327 -0.043767301
## neg_17_pd                                      -0.211202240 -0.060151411
## neg_18_pd                                      -0.194190577 -0.051908104
## neg_19_pd                                      -0.383178158 -0.154364075
## neg_20_pd                                      -0.446272143 -0.193711115
## neg_21_pd                                      -0.297364419 -0.117692721
## neg_22_pd                                      -0.284965743 -0.096423617
## neg_23_pd                                      -0.398786798 -0.116590871
## neg_24_pd                                      -0.517924064 -0.180864324
## neg_25_pd                                      -0.321229170 -0.038788957
## neg_26_pd                                      -0.301468962  0.044238456
## neg_27_pd                                      -0.707552430 -0.213628304
## neg_28_pd                                      -0.663134057 -0.201311333
## neg_29_pd                                      -0.241917565  0.024088229
## neg_30_pd                                      -0.304155495 -0.052096370
## neg_31_pd                                      -0.274766638 -0.054179201
## neg_32_pd                                      -0.177009269  0.032863102
## neg_33_pd                                      -0.212190787  0.109007659
## pos_0_pd                                       -0.117804387 -0.011149669
## pos_1_pd                                       -0.153952985 -0.040113963
## pos_2_pd                                       -0.109271024 -0.005207113
## pos_3_pd                                       -0.147951769 -0.041493922
## pos_4_pd                                       -0.153750936 -0.047126354
## pos_5_pd                                       -0.200386359 -0.092045551
## pos_6_pd                                       -0.210146623 -0.097121035
## pos_7_pd                                       -0.212953830 -0.094523821
## pos_8_pd                                       -0.253243669 -0.140578750
## pos_9_pd                                       -0.280162923 -0.162175014
## pos_10_pd                                      -0.305299298 -0.180016549
## pos_11_pd                                      -0.300854941 -0.183783467
## pos_12_pd                                      -0.316120292 -0.193795841
## pos_13_pd                                      -0.400921985 -0.262320233
## pos_14_pd                                      -0.386047681 -0.247335909
## pos_15_pd                                      -0.385495339 -0.250438341
## pos_16_pd                                      -0.408108802 -0.268524051
## pos_17_pd                                      -0.404219857 -0.263061724
## pos_18_pd                                      -0.398358676 -0.253663735
## pos_19_pd                                      -0.383498238 -0.243608362
## pos_20_pd                                      -0.396695074 -0.247582690
## pos_21_pd                                      -0.432711692 -0.274332431
## pos_22_pd                                      -0.422558279 -0.266749984
## pos_23_pd                                      -0.437700745 -0.273338184
## pos_24_pd                                      -0.485100733 -0.310854938
## pos_25_pd                                      -0.473288282 -0.279921074
## pos_26_pd                                      -0.478654088 -0.289983620
## pos_27_pd                                      -0.538577534 -0.338308822
## pos_28_pd                                      -0.527526463 -0.321207072
## pos_29_pd                                      -0.551613080 -0.334408979
## pos_30_pd                                      -0.510095934 -0.285008197
## pos_31_pd                                      -0.493354996 -0.241545861
## pos_32_pd                                      -0.545086726 -0.298666643
## pos_33_pd                                      -0.632136706 -0.326206090
## pos_34_pd                                      -0.629075408 -0.314276284
## pos_35_pd                                      -0.786883592 -0.514644392
## pos_36_pd                                      -0.801594573 -0.541250661
## pos_37_pd                                      -0.778573832 -0.534987709
## pos_38_pd                                      -0.752518722 -0.513830174
## pos_39_pd                                      -0.840631968 -0.534088308
## s(Time_Period_ID):as.factor(Region)Midwest.1   -0.604810921 -0.494737079
## s(Time_Period_ID):as.factor(Region)Midwest.2   -0.312013934 -0.224608521
## s(Time_Period_ID):as.factor(Region)Midwest.3    0.006447567  0.093175900
## s(Time_Period_ID):as.factor(Region)Midwest.4    0.248768267  0.333711394
## s(Time_Period_ID):as.factor(Region)Midwest.5    0.430895650  0.513087394
## s(Time_Period_ID):as.factor(Region)Midwest.6    0.614550129  0.703168971
## s(Time_Period_ID):as.factor(Region)Midwest.7    0.811163635  0.903771029
## s(Time_Period_ID):as.factor(Region)Midwest.8    1.041776231  1.159056321
## s(Time_Period_ID):as.factor(Region)Midwest.9    0.972590496  1.099762557
## s(Time_Period_ID):as.factor(Region)Northeast.1 -0.855128818 -0.570605352
## s(Time_Period_ID):as.factor(Region)Northeast.2 -0.575219298 -0.351951731
## s(Time_Period_ID):as.factor(Region)Northeast.3  0.137819821  0.305946250
## s(Time_Period_ID):as.factor(Region)Northeast.4  0.071834695  0.234789810
## s(Time_Period_ID):as.factor(Region)Northeast.5  0.186736401  0.353117285
## s(Time_Period_ID):as.factor(Region)Northeast.6  0.441019976  0.593323854
## s(Time_Period_ID):as.factor(Region)Northeast.7  0.824271871  0.987559759
## s(Time_Period_ID):as.factor(Region)Northeast.8  1.248279088  1.424383187
## s(Time_Period_ID):as.factor(Region)Northeast.9  1.041444149  1.204455894
## s(Time_Period_ID):as.factor(Region)South.1     -0.485631359 -0.389922135
## s(Time_Period_ID):as.factor(Region)South.2     -0.207388443 -0.131694247
## s(Time_Period_ID):as.factor(Region)South.3      0.058850086  0.143187991
## s(Time_Period_ID):as.factor(Region)South.4      0.245225395  0.320084111
## s(Time_Period_ID):as.factor(Region)South.5      0.396570600  0.464161403
## s(Time_Period_ID):as.factor(Region)South.6      0.524700283  0.604633801
## s(Time_Period_ID):as.factor(Region)South.7      0.709248609  0.807908570
## s(Time_Period_ID):as.factor(Region)South.8      0.959164552  1.095942563
## s(Time_Period_ID):as.factor(Region)South.9      0.826912089  0.991245595
## s(Time_Period_ID):as.factor(Region)West.1      -0.427179482 -0.308386528
## s(Time_Period_ID):as.factor(Region)West.2      -0.223790527 -0.131400014
## s(Time_Period_ID):as.factor(Region)West.3      -0.020458926  0.069973226
## s(Time_Period_ID):as.factor(Region)West.4       0.155490954  0.230646763
## s(Time_Period_ID):as.factor(Region)West.5       0.276682349  0.357605994
## s(Time_Period_ID):as.factor(Region)West.6       0.375479806  0.468020533
## s(Time_Period_ID):as.factor(Region)West.7       0.468603452  0.566258884
## s(Time_Period_ID):as.factor(Region)West.8       0.603636187  0.719563215
## s(Time_Period_ID):as.factor(Region)West.9       0.586878298  0.704585511
##                                                      ub_coef    sd_coef
## (Intercept)                                    -9.4866868669 0.06176965
## StateAlaska                                     0.2508364222 0.07608223
## StateArizona                                    0.3697648601 0.05708032
## StateArkansas                                  -0.4070125673 0.05562571
## StateCalifornia                                 0.0709005101 0.06803621
## StateColorado                                   0.1250306159 0.06254242
## StateConnecticut                                0.3554532799 0.06789582
## StateDelaware                                   0.2896640143 0.06867237
## StateFlorida                                    0.5314714116 0.06048309
## StateGeorgia                                    0.2413124953 0.05801161
## StateHawaii                                    -0.3259358787 0.07204827
## StateIdaho                                     -0.0427818655 0.05932911
## StateIllinois                                   0.3623509004 0.06569857
## StateIndiana                                    0.1757559184 0.05085918
## StateIowa                                      -0.5156647960 0.05543271
## StateKansas                                    -0.1281496232 0.04918527
## StateKentucky                                   0.7816901223 0.04831323
## StateLouisiana                                  0.4963074142 0.05174436
## StateMaine                                      0.2135849575 0.07077915
## StateMaryland                                  -1.2312695688 0.11254197
## StateMassachusetts                              0.1329541565 0.11021262
## StateMichigan                                   0.1463224759 0.04884989
## StateMinnesota                                 -0.5370613641 0.05519896
## StateMississippi                                0.0345565579 0.06215570
## StateMissouri                                   0.2850035798 0.05066379
## StateMontana                                   -0.2794470485 0.05861454
## StateNebraska                                  -0.8072931523 0.05354155
## StateNevada                                     0.6053563884 0.05596092
## StateNew Hampshire                              0.2369884117 0.05734516
## StateNew Jersey                                 0.3000758093 0.07874901
## StateNew Mexico                                 0.7556467656 0.06807972
## StateNew York                                  -0.0135086105 0.06059754
## StateNorth Carolina                             0.3615248024 0.04444511
## StateNorth Dakota                              -1.0040184526 0.08606604
## StateOhio                                       0.7226252666 0.06809876
## StateOklahoma                                   0.5683887208 0.05881186
## StateOregon                                    -0.1828275119 0.05930222
## StatePennsylvania                               0.7939018648 0.05940936
## StateRhode Island                              -0.0388844005 0.13961386
## StateSouth Carolina                             0.2755164455 0.05835838
## StateSouth Dakota                              -0.9532062268 0.06717587
## StateTennessee                                  0.5579730566 0.04451192
## StateTexas                                      0.2072279669 0.06109206
## StateUtah                                       0.1306139757 0.09421214
## StateVermont                                   -0.0488460423 0.07257225
## StateVirginia                                   0.1307685399 0.05069525
## StateWashington                                 0.1435274614 0.06093929
## StateWest Virginia                              0.9094816603 0.07429560
## StateWisconsin                                  0.1507264367 0.04866094
## StateWyoming                                    0.1025406352 0.06602185
## Naloxone_Pharmacy_Yes_Redefined                 0.0033057204 0.03297216
## Naloxone_Pharmacy_No_Redefined                  0.0677033095 0.03282138
## Medical_Marijuana_Redefined                     0.2681552721 0.03524732
## Recreational_Marijuana_Redefined               -0.0228118260 0.03868097
## GSL_Redefined                                   0.1181258621 0.02798080
## PDMP_Redefined                                 -0.1236307979 0.02810348
## Medicaid_Expansion_Redefined                    0.1403292147 0.02608843
## neg_2_pd                                        0.1318919996 0.05682003
## neg_3_pd                                        0.1267811550 0.05613472
## neg_4_pd                                        0.1085573626 0.06088835
## neg_5_pd                                        0.1027199694 0.05904333
## neg_6_pd                                        0.1040063133 0.05601269
## neg_7_pd                                        0.0711037058 0.05873735
## neg_8_pd                                        0.0422047875 0.07283109
## neg_9_pd                                        0.1297157295 0.07901769
## neg_10_pd                                       0.1650766271 0.07196187
## neg_11_pd                                       0.1741831221 0.07590357
## neg_12_pd                                       0.2787748743 0.08333640
## neg_13_pd                                       0.1809790428 0.08459273
## neg_14_pd                                       0.1411791778 0.08005551
## neg_15_pd                                       0.1271533044 0.09522889
## neg_16_pd                                       0.1276287244 0.08744695
## neg_17_pd                                       0.0908994188 0.07706675
## neg_18_pd                                       0.0903743687 0.07259310
## neg_19_pd                                       0.0744500090 0.11674188
## neg_20_pd                                       0.0588499140 0.12885767
## neg_21_pd                                       0.0619789771 0.09166923
## neg_22_pd                                       0.0921185097 0.09619496
## neg_23_pd                                       0.1656050554 0.14397751
## neg_24_pd                                       0.1561954167 0.17196926
## neg_25_pd                                       0.2436512570 0.14410215
## neg_26_pd                                       0.3899458747 0.17638134
## neg_27_pd                                       0.2802958226 0.25200211
## neg_28_pd                                       0.2605113905 0.23562384
## neg_29_pd                                       0.2900940227 0.13571724
## neg_30_pd                                       0.1999627543 0.12860159
## neg_31_pd                                       0.1664082361 0.11254461
## neg_32_pd                                       0.2427354736 0.10707774
## neg_33_pd                                       0.4302061063 0.16387676
## pos_0_pd                                        0.0955050485 0.05441567
## pos_1_pd                                        0.0737250597 0.05808113
## pos_2_pd                                        0.0988567975 0.05309383
## pos_3_pd                                        0.0649639253 0.05431523
## pos_4_pd                                        0.0594982288 0.05440030
## pos_5_pd                                        0.0162952567 0.05527592
## pos_6_pd                                        0.0159045526 0.05766612
## pos_7_pd                                        0.0239061876 0.06042347
## pos_8_pd                                       -0.0279138306 0.05748210
## pos_9_pd                                       -0.0441871036 0.06019791
## pos_10_pd                                      -0.0547338000 0.06391977
## pos_11_pd                                      -0.0667119929 0.05973034
## pos_12_pd                                      -0.0714713887 0.06241043
## pos_13_pd                                      -0.1237184816 0.07071518
## pos_14_pd                                      -0.1086241379 0.07077131
## pos_15_pd                                      -0.1153813424 0.06890663
## pos_16_pd                                      -0.1289392991 0.07121671
## pos_17_pd                                      -0.1219035922 0.07201946
## pos_18_pd                                      -0.1089687946 0.07382395
## pos_19_pd                                      -0.1037184858 0.07137239
## pos_20_pd                                      -0.0984703060 0.07607775
## pos_21_pd                                      -0.1159531705 0.08080575
## pos_22_pd                                      -0.1109416884 0.07949403
## pos_23_pd                                      -0.1089756223 0.08385845
## pos_24_pd                                      -0.1366091424 0.08890092
## pos_25_pd                                      -0.0865538659 0.09865674
## pos_26_pd                                      -0.1013131529 0.09626044
## pos_27_pd                                      -0.1380401111 0.10217791
## pos_28_pd                                      -0.1148876812 0.10526500
## pos_29_pd                                      -0.1172048769 0.11081842
## pos_30_pd                                      -0.0599204594 0.11484068
## pos_31_pd                                       0.0102632731 0.12847405
## pos_32_pd                                      -0.0522465594 0.12572453
## pos_33_pd                                      -0.0202754733 0.15608705
## pos_34_pd                                       0.0005228401 0.16061180
## pos_35_pd                                      -0.2424051926 0.13889755
## pos_36_pd                                      -0.2809067482 0.13282853
## pos_37_pd                                      -0.2914015860 0.12427863
## pos_38_pd                                      -0.2751416257 0.12177987
## pos_39_pd                                      -0.2275446476 0.15639983
## s(Time_Period_ID):as.factor(Region)Midwest.1   -0.3846632372 0.05616012
## s(Time_Period_ID):as.factor(Region)Midwest.2   -0.1372031085 0.04459460
## s(Time_Period_ID):as.factor(Region)Midwest.3    0.1799042339 0.04424915
## s(Time_Period_ID):as.factor(Region)Midwest.4    0.4186545216 0.04333833
## s(Time_Period_ID):as.factor(Region)Midwest.5    0.5952791384 0.04193456
## s(Time_Period_ID):as.factor(Region)Midwest.6    0.7917878132 0.04521369
## s(Time_Period_ID):as.factor(Region)Midwest.7    0.9963784224 0.04724867
## s(Time_Period_ID):as.factor(Region)Midwest.8    1.2763364104 0.05983678
## s(Time_Period_ID):as.factor(Region)Midwest.9    1.2269346171 0.06488370
## s(Time_Period_ID):as.factor(Region)Northeast.1 -0.2860818865 0.14516503
## s(Time_Period_ID):as.factor(Region)Northeast.2 -0.1286841648 0.11391202
## s(Time_Period_ID):as.factor(Region)Northeast.3  0.4740726796 0.08577879
## s(Time_Period_ID):as.factor(Region)Northeast.4  0.3977449240 0.08314036
## s(Time_Period_ID):as.factor(Region)Northeast.5  0.5194981681 0.08488821
## s(Time_Period_ID):as.factor(Region)Northeast.6  0.7456277323 0.07770606
## s(Time_Period_ID):as.factor(Region)Northeast.7  1.1508476467 0.08331015
## s(Time_Period_ID):as.factor(Region)Northeast.8  1.6004872852 0.08984903
## s(Time_Period_ID):as.factor(Region)Northeast.9  1.3674676378 0.08316926
## s(Time_Period_ID):as.factor(Region)South.1     -0.2942129097 0.04883124
## s(Time_Period_ID):as.factor(Region)South.2     -0.0560000507 0.03861949
## s(Time_Period_ID):as.factor(Region)South.3      0.2275258950 0.04302954
## s(Time_Period_ID):as.factor(Region)South.4      0.3949428261 0.03819322
## s(Time_Period_ID):as.factor(Region)South.5      0.5317522072 0.03448510
## s(Time_Period_ID):as.factor(Region)South.6      0.6845673191 0.04078241
## s(Time_Period_ID):as.factor(Region)South.7      0.9065685299 0.05033671
## s(Time_Period_ID):as.factor(Region)South.8      1.2327205728 0.06978470
## s(Time_Period_ID):as.factor(Region)South.9      1.1555791005 0.08384363
## s(Time_Period_ID):as.factor(Region)West.1      -0.1895935737 0.06060865
## s(Time_Period_ID):as.factor(Region)West.2      -0.0390095014 0.04713802
## s(Time_Period_ID):as.factor(Region)West.3       0.1604053778 0.04613885
## s(Time_Period_ID):as.factor(Region)West.4       0.3058025724 0.03834480
## s(Time_Period_ID):as.factor(Region)West.5       0.4385296390 0.04128757
## s(Time_Period_ID):as.factor(Region)West.6       0.5605612606 0.04721466
## s(Time_Period_ID):as.factor(Region)West.7       0.6639143157 0.04982420
## s(Time_Period_ID):as.factor(Region)West.8       0.8354902433 0.05914644
## s(Time_Period_ID):as.factor(Region)West.9       0.8222927245 0.06005470
# write.csv(format(round(sensitivity_anlys_event_study_sd_and_ci, 3), nsmall = 3), "./Data/event_study_coef_and_ci.csv")

6.4.3 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_event_study_log_smoothed_time <- sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time %>%
  mutate(term = rownames(sensitivity_anlys_event_study_sd_and_ci_log_smoothed_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}), 
                     sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")})))
colnames(plot_event_study_log_smoothed_time) <- c("term", "estimate", "conf.low", "conf.high")

dwplot(plot_event_study_log_smoothed_time, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Pre-Intervention and Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

6.4.4 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_smoothed_eff_log_outcome_event_study <- boostrap_state_time(sensitivity_anlys_event_study_data,
                                                                      sensitivity_anlys_event_study_model_log_smoothed_time,
                                                          lin_model = FALSE)

reduced_coef_event_study <- Reduce("cbind", bootstrap_smoothed_eff_log_outcome_event_study[[1]])
coef_w_ci <- cbind(t(apply(reduced_coef_event_study, 1, quantile, probs = c(.025, .975))), apply(reduced_coef_event_study, 1, mean))
colnames(coef_w_ci) <- c("conf.low", "conf.high", "estimate")
coef_w_ci <- data.frame(coef_w_ci)
coef_w_ci$term <- rownames(coef_w_ci)

dwplot(coef_w_ci, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Pre-Intervention and Post-Intervention Periods, Using Bootstrap") + 
  scale_color_grey() + 
  coord_flip() +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

6.5 Analysis With Only Periods After Treatment

formula_post_tx_log_smoothed_time <- formula(paste("log(prop_dead)~ State +
                                           s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                     paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                              function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_post_tx_model_log_smoothed_time<-gam(formula_post_tx_log_smoothed_time,
                                         data = sensitivity_anlys_event_study_data)
# summary(sensitivity_anlys_post_tx_model_log_smoothed_time)

6.5.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_post_tx_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time <- coef(sensitivity_anlys_post_tx_model_log_smoothed_time)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time <- 
  compute_sd_and_CI(as.matrix(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time), 
                    log(sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time, 
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time) - 50)
# sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time

6.5.2 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_post_tx_log_smoothed_time <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time %>%
  mutate(term = rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) 
colnames(plot_post_tx_log_smoothed_time) <- c("term", "estimate", "conf.low", "conf.high")
plot_post_tx_log_smoothed_time$num_states <- sapply(plot_post_tx_log_smoothed_time$term,
                                                    function(x){sum(sensitivity_anlys_event_study_data[,x])})

dwplot(plot_post_tx_log_smoothed_time, colour = "black",
       vars_order =  c(sapply(((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0), 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45, size = 4)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time Periods", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() 

  # geom_vline(aes(xintercept = coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"]), linetype = "dashed", color = "red") +
  # geom_text(aes(label = paste("Coefficient Estimate: ", coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"]), y = 12, 
  #           x = coef(main_analysis_model_log_smoothed_time)["Intervention_Redefined"] + 0.1), color = "red")
  # geom_text(aes(label = num_states, x = .1, y = 40:1), size = 2)

6.5.3 Attributable Deaths

date_data <- sensitivity_anlys_event_study_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
colnames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high", "sd")
sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time)
attr_deaths_est_log_smoothed_time_post <- attr_death_compute(sensitivity_anlys_event_study_data,
                                                                sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time,
                                                                lin_model = FALSE)

attr_deaths_est_log_smoothed_time_post <- merge(attr_deaths_est_log_smoothed_time_post, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_smoothed_time_post, aes(x = Time_Period_Start)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Event Study",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.6 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_smoothed_eff_log_outcome_post_tx <- boostrap_state_time(sensitivity_anlys_event_study_data,
                                                                  sensitivity_anlys_post_tx_model_log_smoothed_time,
                                                                  lin_model = FALSE)

reduced_coef_outcome_post_tx <- Reduce("cbind", bootstrap_smoothed_eff_log_outcome_post_tx[[1]])

coef_w_ci_smoothed_time_log_outcome_post_tx <- cbind(t(apply(reduced_coef_outcome_post_tx, 1, quantile, probs = c(.025, .975))),
                                             apply(reduced_coef_outcome_post_tx, 1, mean))
colnames(coef_w_ci_smoothed_time_log_outcome_post_tx) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_smoothed_time_log_outcome_post_tx <- data.frame(coef_w_ci_smoothed_time_log_outcome_post_tx)
coef_w_ci_smoothed_time_log_outcome_post_tx$term <- rownames(coef_w_ci_smoothed_time_log_outcome_post_tx)

dwplot(coef_w_ci_smoothed_time_log_outcome_post_tx, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Post-Intervention Periods, Using Bootstrap") + 
  scale_color_grey() + 
  coord_flip() 

list_attr_deaths_post_tx <- lapply(bootstrap_smoothed_eff_log_outcome_post_tx[[2]], function(x){x$attr_deaths})
reduced_attr_deaths_post_tx <- Reduce("cbind", list_attr_deaths_post_tx)
attr_deaths_smoothed_time_post_tx <- cbind(t(apply(reduced_attr_deaths_post_tx, 1, quantile, probs = c(.025, .975), na.rm = TRUE)), 
                                               apply(reduced_attr_deaths_post_tx, 1, mean, na.rm = TRUE))
colnames(attr_deaths_smoothed_time_post_tx) <- c("conf.low", "conf.high", "estimate")
attr_deaths_smoothed_time_post_tx <- data.frame(attr_deaths_smoothed_time_post_tx)
attr_deaths_smoothed_time_post_tx$Time_Period_Start <- unique(main_analysis_data$Time_Period_Start)

ggplot(attr_deaths_smoothed_time_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.6.1 Attributable Deaths

colnames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high")
sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time)
date_data <- sensitivity_anlys_event_study_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_smoothed_time_post_tx <- attr_death_compute(sensitivity_anlys_event_study_data,
                                                                sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time)
attr_deaths_est_log_smoothed_time_post_tx <- merge(attr_deaths_est_log_smoothed_time_post_tx, date_data, 
                                               by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_smoothed_time_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Semi-Dynamic Model",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.6.2 Model With Semi Dynamic Regression

coef_semi_dynamic <- data.frame(matrix(NA, nrow = 40, ncol = 4))
for(time in 0:39){
  subset_data <- sensitivity_anlys_event_study_data %>%
    filter(get(paste("pos_", time, "_pd", sep = "")) == 1 | 
             Time_Period_Start <= Intervention_First_Date)
  formula_semi_dynamic <- formula(paste("log(prop_dead)~ State +
                                           s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                        paste("pos_", time, "_pd", sep = "")))
  #run the gam model
  semi_dynamic_model<-gam(formula_semi_dynamic, data = subset_data)
  summary_semi_dynamic_model <- summary(semi_dynamic_model)
  sd_semi_dynamic_model <- summary_semi_dynamic_model$se[paste("pos_", time, "_pd", sep = "")]
  
  coef_value <- coef(semi_dynamic_model)[paste("pos_", time, "_pd", sep = "")]
  
  coef_semi_dynamic[time + 1,] <- c(time, coef_value, coef_value - 1.96*sd_semi_dynamic_model, coef_value + 1.96*sd_semi_dynamic_model)
}

colnames(coef_semi_dynamic) <- c("time_after_tx", "estimate", "lb", "ub")

ggplot(coef_semi_dynamic, aes(y = estimate, x = time_after_tx)) + 
  geom_pointrange(aes(ymin = lb, ymax = ub), fatten = 1)

6.7 Analysis With Linear Effects Periods After Treatment

#use this function to compute the cumulative sum, but resets the sum if the variable was 0
compute_cumsum = function(x){
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
}
sensitivity_anlys_event_study_data_lin_post_tx <- sensitivity_anlys_event_study_data %>%
  arrange(State, Time_Period_ID) %>%
  group_by(State) %>%
  mutate(sum_tx_periods = pos_0_pd + pos_1_pd + pos_2_pd + pos_3_pd + 
           pos_4_pd + pos_5_pd + pos_6_pd + pos_7_pd + pos_8_pd + pos_9_pd + 
           pos_10_pd + pos_11_pd + pos_12_pd + pos_13_pd + pos_14_pd + 
           pos_15_pd + pos_16_pd + pos_17_pd + pos_18_pd + pos_19_pd + 
           pos_20_pd + pos_21_pd + pos_22_pd + pos_23_pd + pos_24_pd + 
           pos_25_pd + pos_26_pd + pos_27_pd + pos_28_pd + pos_29_pd + 
           pos_30_pd + pos_31_pd + pos_32_pd + pos_33_pd + pos_34_pd + 
           pos_35_pd + pos_36_pd + pos_37_pd + pos_38_pd + pos_39_pd,
         time_after_tx = cumsum(sum_tx_periods),
         num_pd_w_tx = compute_cumsum(Intervention_Redefined ),
         num_pd_w_naloxone_yes = compute_cumsum(Naloxone_Pharmacy_Yes_Redefined),
         num_pd_w_naloxone_no = compute_cumsum(Naloxone_Pharmacy_No_Redefined),
         num_pd_w_med_marijuana = compute_cumsum(Medical_Marijuana_Redefined),
         num_pd_w_rec_marijuana = compute_cumsum(Recreational_Marijuana_Redefined),
         num_pd_w_gsl = compute_cumsum(GSL_Redefined),
         num_pd_w_pdmp = compute_cumsum(PDMP_Redefined),
         num_pd_w_medicaid = compute_cumsum(Medicaid_Expansion_Redefined),
         lag_num_pd_w_tx = lag(num_pd_w_tx),
         lag_num_pd_w_naloxone_yes = lag(num_pd_w_naloxone_yes),
         lag_num_pd_w_naloxone_no = lag(num_pd_w_naloxone_no),
         lag_num_pd_w_med_marijuana = lag(num_pd_w_med_marijuana),
         lag_num_pd_w_rec_marijuana = lag(num_pd_w_rec_marijuana),
         lag_num_pd_w_gsl = lag(num_pd_w_gsl),
         lag_num_pd_w_pdmp = lag(num_pd_w_pdmp),
         lag_num_pd_w_medicaid = lag(num_pd_w_medicaid)) #lag so that intercept = effect when tx first occurs

#fill in a 0 for the NAs so we keep all the data and at most this will be 0
sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_tx[is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_tx)] <- 
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_yes[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_yes)]<-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_no[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_naloxone_no)] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_med_marijuana[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_med_marijuana)] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_rec_marijuana[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_rec_marijuana)] <- 
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_gsl[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_gsl)] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_pdmp[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_pdmp)] <-
  sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_medicaid[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx$lag_num_pd_w_medicaid)] <-0

#run the gam model
sensitivity_anlys_lin_post_tx_model_log_smoothed_time<-gam(log(prop_dead)~ State +
                                                             s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                             Naloxone_Pharmacy_Yes_Redefined + 
                                                             lag_num_pd_w_naloxone_yes +
                                                             Naloxone_Pharmacy_No_Redefined + 
                                                             lag_num_pd_w_naloxone_no +
                                                             Medical_Marijuana_Redefined + 
                                                             lag_num_pd_w_med_marijuana +
                                                             Recreational_Marijuana_Redefined + 
                                                             lag_num_pd_w_rec_marijuana +
                                                             GSL_Redefined + 
                                                             lag_num_pd_w_gsl +
                                                             PDMP_Redefined + 
                                                             lag_num_pd_w_pdmp +
                                                             Medicaid_Expansion_Redefined +
                                                             lag_num_pd_w_medicaid +
                                                             Intervention_Redefined +
                                                             lag_num_pd_w_tx,
                                                           data = sensitivity_anlys_event_study_data_lin_post_tx)
summary(sensitivity_anlys_lin_post_tx_model_log_smoothed_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + Intervention_Redefined + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.679242   0.062703 -154.367  < 2e-16 ***
## StateAlaska                       0.171067   0.083623    2.046 0.040923 *  
## StateArizona                      0.263469   0.067148    3.924 9.03e-05 ***
## StateArkansas                    -0.505101   0.068258   -7.400 2.03e-13 ***
## StateCalifornia                   0.055219   0.086222    0.640 0.521972    
## StateColorado                     0.082095   0.079386    1.034 0.301209    
## StateConnecticut                  0.273879   0.073642    3.719 0.000206 ***
## StateDelaware                     0.172479   0.069552    2.480 0.013229 *  
## StateFlorida                      0.486509   0.069486    7.002 3.49e-12 ***
## StateGeorgia                      0.255774   0.071565    3.574 0.000360 ***
## StateHawaii                      -0.422570   0.085574   -4.938 8.58e-07 ***
## StateIdaho                       -0.275520   0.070370   -3.915 9.35e-05 ***
## StateIllinois                     0.303208   0.074210    4.086 4.57e-05 ***
## StateIndiana                     -0.037737   0.069067   -0.546 0.584865    
## StateIowa                        -0.606970   0.065634   -9.248  < 2e-16 ***
## StateKansas                      -0.193152   0.065147   -2.965 0.003066 ** 
## StateKentucky                     0.588462   0.069682    8.445  < 2e-16 ***
## StateLouisiana                    0.428767   0.065007    6.596 5.46e-11 ***
## StateMaine                        0.224105   0.083143    2.695 0.007092 ** 
## StateMaryland                    -1.392600   0.071365  -19.514  < 2e-16 ***
## StateMassachusetts               -0.170217   0.070051   -2.430 0.015195 *  
## StateMichigan                     0.016565   0.072579    0.228 0.819491    
## StateMinnesota                   -0.552033   0.071513   -7.719 1.87e-14 ***
## StateMississippi                 -0.139820   0.064841   -2.156 0.031181 *  
## StateMissouri                     0.308393   0.074569    4.136 3.69e-05 ***
## StateMontana                     -0.208797   0.075734   -2.757 0.005889 ** 
## StateNebraska                    -0.904531   0.068537  -13.198  < 2e-16 ***
## StateNevada                       0.555325   0.081298    6.831 1.13e-11 ***
## StateNew Hampshire                0.177347   0.069202    2.563 0.010461 *  
## StateNew Jersey                   0.243303   0.068786    3.537 0.000414 ***
## StateNew Mexico                   0.690637   0.080021    8.631  < 2e-16 ***
## StateNew York                    -0.194746   0.071279   -2.732 0.006350 ** 
## StateNorth Carolina               0.315352   0.064605    4.881 1.14e-06 ***
## StateNorth Dakota                -1.215966   0.065190  -18.653  < 2e-16 ***
## StateOhio                         0.638098   0.069233    9.217  < 2e-16 ***
## StateOklahoma                     0.366472   0.069401    5.281 1.44e-07 ***
## StateOregon                      -0.083960   0.081922   -1.025 0.305548    
## StatePennsylvania                 0.659468   0.072565    9.088  < 2e-16 ***
## StateRhode Island                -0.355684   0.074734   -4.759 2.09e-06 ***
## StateSouth Carolina               0.118337   0.065579    1.804 0.071311 .  
## StateSouth Dakota                -1.086577   0.068067  -15.963  < 2e-16 ***
## StateTennessee                    0.455756   0.064487    7.067 2.20e-12 ***
## StateTexas                        0.081115   0.072106    1.125 0.260757    
## StateUtah                        -0.128153   0.069487   -1.844 0.065296 .  
## StateVermont                     -0.087964   0.070929   -1.240 0.215068    
## StateVirginia                     0.028509   0.066509    0.429 0.668227    
## StateWashington                   0.131122   0.080705    1.625 0.104390    
## StateWest Virginia                0.623536   0.070155    8.888  < 2e-16 ***
## StateWisconsin                    0.118111   0.067104    1.760 0.078549 .  
## StateWyoming                     -0.071053   0.064853   -1.096 0.273390    
## Naloxone_Pharmacy_Yes_Redefined  -0.046045   0.042229   -1.090 0.275692    
## lag_num_pd_w_naloxone_yes        -0.029879   0.006669   -4.480 7.89e-06 ***
## Naloxone_Pharmacy_No_Redefined    0.091093   0.041476    2.196 0.028190 *  
## lag_num_pd_w_naloxone_no         -0.019468   0.004063   -4.791 1.78e-06 ***
## Medical_Marijuana_Redefined       0.190424   0.030399    6.264 4.62e-10 ***
## lag_num_pd_w_med_marijuana       -0.008770   0.002197   -3.991 6.83e-05 ***
## Recreational_Marijuana_Redefined -0.014750   0.061944   -0.238 0.811820    
## lag_num_pd_w_rec_marijuana       -0.008046   0.011015   -0.731 0.465165    
## GSL_Redefined                     0.076724   0.033288    2.305 0.021284 *  
## lag_num_pd_w_gsl                  0.011539   0.004089    2.822 0.004821 ** 
## PDMP_Redefined                   -0.133325   0.027457   -4.856 1.30e-06 ***
## lag_num_pd_w_pdmp                 0.006924   0.002392    2.894 0.003844 ** 
## Medicaid_Expansion_Redefined      0.082070   0.033950    2.417 0.015726 *  
## lag_num_pd_w_medicaid             0.012350   0.005043    2.449 0.014419 *  
## Intervention_Redefined           -0.055814   0.024061   -2.320 0.020462 *  
## lag_num_pd_w_tx                  -0.014751   0.001785   -8.263 2.63e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.211  5.202 97.12  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.502  8.928 52.42  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.153  7.312 58.06  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      4.103  5.096 41.15  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.85   Deviance explained = 85.6%
## GCV = 0.084518  Scale est. = 0.080758  n = 2000
plot(sensitivity_anlys_lin_post_tx_model_log_smoothed_time, pages = 1)

6.7.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time <-
  data.frame(predict(sensitivity_anlys_lin_post_tx_model_log_smoothed_time, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_lin_post_tx_log_smoothed_time <- coef(sensitivity_anlys_lin_post_tx_model_log_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time <- 
  compute_sd_and_CI(as.matrix(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time), 
                    log(sensitivity_anlys_event_study_data$prop_dead),
                    coefficient_values_sensitivity_anlys_lin_post_tx_log_smoothed_time, 
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_lin_post_tx_log_smoothed_time) - 50)
# format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time, 3), nsmall = 3)

colnames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time) <- c("conf.low", "estimate", "conf.high", "sd")
sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$term <- rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time)

sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention") + 
  scale_color_grey() + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

6.7.2 Attributable Deaths

date_data <- sensitivity_anlys_event_study_data_lin_post_tx[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_smoothed_time_lin_post <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx,
                                                                sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time,
                                                        lin_model = TRUE, tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))

attr_deaths_est_log_smoothed_time_lin_post <- merge(attr_deaths_est_log_smoothed_time_lin_post, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

attr_deaths_est_log_smoothed_time_lin_post_summary <- attr_deaths_est_log_smoothed_time_lin_post %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_attr_deaths = sum(attr_deaths),
            total_attr_deaths_lb = sum(attr_deaths_lb),
            total_attr_deaths_ub = sum(attr_deaths_ub))

ggplot(attr_deaths_est_log_smoothed_time_lin_post_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year,
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

#overall national overdose deaths
national_od <- sensitivity_anlys_event_study_data_lin_post_tx %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_od = sum(imputed_deaths),
            total_od_prob = sum(imputed_deaths)/sum(population),
            total_pop = sum(population))



national_od <- merge(national_od, attr_deaths_est_log_smoothed_time_lin_post_summary, by = "year")

ggplot(national_od, aes(x = year)) + 
  geom_line(aes(y = total_od, color = "Oberved OD")) +
  geom_line(aes(y = total_attr_deaths, color = "Lives Saved", linetype = "Estimate")) + 
  geom_line(aes(y = total_attr_deaths_lb, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths_ub, color = "Lives Saved", linetype = "95% CI")) + 
  geom_line(aes(y = total_attr_deaths + total_od, 
                color = "Potential Number of Deaths Had There Not Been DIH Prosecutions")) + 
  labs(x = "Date", y = "People",
       title = "Number of OD Deaths and Estimated Number of Lives Saved in Each Year",
       color = "", linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))

6.7.3 Bootstrapping: Coefficient and Attributable Death Estimate

6.7.3.1 Clustered

bootstrap_smoothed_eff_log_outcome_lin_post_tx <- boostrap_state_time_group(sensitivity_anlys_event_study_data_lin_post_tx,
                                                                  sensitivity_anlys_lin_post_tx_model_log_smoothed_time, 
                       coef_of_interest = rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,]),
                       nSim = 5000)
colnames(bootstrap_smoothed_eff_log_outcome_lin_post_tx) <- rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,])

# write.csv(bootstrap_smoothed_eff_log_outcome_lin_post_tx,
#           "./Data/all_clustered_bootstrap_smoothed_time_lin_tx_3_11_22_nSim_5000.csv",
#           row.names = FALSE)

mean_coef_outcome_lin_post_tx <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx, 2, mean)
ci_coef_outcome_lin_post_tx <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx, 2, quantile, probs = c(.025, .975))


coef_w_ci_smoothed_time_log_outcome_lin_post_tx <- cbind(t(ci_coef_outcome_lin_post_tx),
                                             mean_coef_outcome_lin_post_tx)
colnames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx <- data.frame(coef_w_ci_smoothed_time_log_outcome_lin_post_tx)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx$ci_95 <- paste("95% CI = (", 
                                                               format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx$conf.low, 3),
                                                                      nsmall = 3),
                                                               ",",
                                                               format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx$conf.high,3),
                                                                      nsmall = 3),
                                                               ")", sep = "")
# write.csv(coef_w_ci_smoothed_time_log_outcome_lin_post_tx,
#           "./Data/clustered_bootstrap_summary_smoothed_time_lin_tx_3_11_22_nSim_5000.csv",
#           row.names = FALSE)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx <-
  read.csv("./Data/clustered_bootstrap_summary_smoothed_time_lin_tx_3_11_22_nSim_5000.csv",
                                                            row.names = "term")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx)

#multiply coefficients by 10 if they're the linear effects
ten_pd_eff <- coef_w_ci_smoothed_time_log_outcome_lin_post_tx[rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx) %in%
                                                                c("lag_num_pd_w_naloxone_yes",
                                                                  "lag_num_pd_w_naloxone_no",
                                                                  "lag_num_pd_w_med_marijuana",
                                                                  "lag_num_pd_w_rec_marijuana",
                                                                  "lag_num_pd_w_gsl",
                                                                  "lag_num_pd_w_pdmp",
                                                                  "lag_num_pd_w_medicaid",
                                                                  "lag_num_pd_w_tx"),]

ten_pd_eff$estimate <- ten_pd_eff$estimate*10
ten_pd_eff$conf.high <- ten_pd_eff$conf.high*10
ten_pd_eff$conf.low <- ten_pd_eff$conf.low*10
ten_pd_eff$ci_95 <- paste("95% CI = (", format(round(ten_pd_eff$conf.low, 3), nsmall = 3), ", ", format(round(ten_pd_eff$conf.high, 3),
                                                                                                        nsmall = 3), ")", sep = "")
rownames(ten_pd_eff) <- ten_pd_eff$term <- c("num_pd_w_naloxone_yes_per_5_yr",
                                             "num_pd_w_naloxone_no_per_5_yr",
                                             "num_pd_w_med_marijuana_per_5_yr",
                                             "num_pd_w_rec_marijuana_per_5_yr",
                                             "num_pd_w_gsl_per_5_yr",
                                             "num_pd_w_pdmp_per_5_yr",
                                             "num_pd_w_medicaid_per_5_yr",
                                             "num_pd_w_tx_per_5_yr")

combined_ten_pd_df <- rbind(coef_w_ci_smoothed_time_log_outcome_lin_post_tx, ten_pd_eff)
combined_ten_pd_df_to_be_combined <- combined_ten_pd_df %>%
  filter(!(term %in% c("lag_num_pd_w_naloxone_yes",
                       "lag_num_pd_w_naloxone_no",
                       "lag_num_pd_w_med_marijuana",
                       "lag_num_pd_w_rec_marijuana",
                       "lag_num_pd_w_gsl",
                       "lag_num_pd_w_pdmp",
                       "lag_num_pd_w_medicaid",
                       "lag_num_pd_w_tx")))

#put model estimates in the plot
model_effect_estimates <- sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,]
model_effect_estimates_to_be_combined <- model_effect_estimates[
  !rownames(model_effect_estimates) %in%
    c("lag_num_pd_w_naloxone_yes",
      "lag_num_pd_w_naloxone_no",
      "lag_num_pd_w_med_marijuana",
      "lag_num_pd_w_rec_marijuana",
      "lag_num_pd_w_gsl",
      "lag_num_pd_w_pdmp",
      "lag_num_pd_w_medicaid",
      "lag_num_pd_w_tx"),]
combined_ten_pd_model_est <- sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[
  rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time) %in%
    c("lag_num_pd_w_naloxone_yes",
      "lag_num_pd_w_naloxone_no",
      "lag_num_pd_w_med_marijuana",
      "lag_num_pd_w_rec_marijuana",
      "lag_num_pd_w_gsl",
      "lag_num_pd_w_pdmp",
      "lag_num_pd_w_medicaid",
      "lag_num_pd_w_tx"),]
rownames(combined_ten_pd_model_est) <- combined_ten_pd_model_est$term <- c("num_pd_w_naloxone_yes_per_5_yr",
                                             "num_pd_w_naloxone_no_per_5_yr",
                                             "num_pd_w_med_marijuana_per_5_yr",
                                             "num_pd_w_rec_marijuana_per_5_yr",
                                             "num_pd_w_gsl_per_5_yr",
                                             "num_pd_w_pdmp_per_5_yr",
                                             "num_pd_w_medicaid_per_5_yr",
                                             "num_pd_w_tx_per_5_yr")
combined_ten_pd_model_est$estimate <- combined_ten_pd_model_est$estimate*10
combined_ten_pd_model_est$conf.low <- combined_ten_pd_model_est$conf.low*10
combined_ten_pd_model_est$conf.high <- combined_ten_pd_model_est$conf.high*10
combined_ten_pd_model_est$ci_95 <- paste("95% CI = (", 
                                         format(round(combined_ten_pd_model_est$conf.low, 3), nsmall = 3),
                                         ", ",
                                         format(round(combined_ten_pd_model_est$conf.high, 3), nsmall = 3), ")", sep = "")

model_effect_estimates_to_be_combined <- rbind(model_effect_estimates_to_be_combined, combined_ten_pd_model_est)

ggplot() +  
  geom_point(combined_ten_pd_df_to_be_combined, mapping = aes(x = estimate, y = 16:1, color = "Bootstrap Estimate")) + 
  geom_linerange(combined_ten_pd_df_to_be_combined, mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1, color = "Bootstrap Estimate")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(color = "black"),
        legend.position = "bottom") +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  geom_point(model_effect_estimates_to_be_combined, 
             mapping = aes(x = estimate, y = 16:1, color = "Model Estimate")) + 
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Model Coefficient and Bootstrap 95% CI of 
       Model Using Smoothed Time Effects, 
       Linear Policy Effects, 
       Using Clustered Bootstrap",
       color = "Estimate Type") + 
  # scale_color_grey() + 
  geom_text(model_effect_estimates_to_be_combined, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.8, y = 16:1), size = 3) + 
  geom_text(combined_ten_pd_df_to_be_combined, 
            mapping = aes(label = ci_95, x = 1.35, y = 16:1), size = 3) +
  xlim(-.8, 1.7) + 
  scale_y_continuous(breaks = 1:16, labels = combined_ten_pd_df_to_be_combined$term[16:1])

ggplot() +  
  # geom_point(combined_ten_pd_df, mapping = aes(x = estimate, y = 16:1, color = "Bootstrap Estimate")) + 
  geom_linerange(combined_ten_pd_df_to_be_combined, mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1)) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(color = "black"),
        legend.position = "bottom") +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  geom_point(model_effect_estimates_to_be_combined, 
             mapping = aes(x = estimate, y = 16:1)) + 
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Model Coefficient and Bootstrap 95% CI of 
       Model Using Smoothed Time Effects, 
       Linear Policy Effects, 
       Using Clustered Bootstrap",
       color = "Estimate Type") + 
  # scale_color_grey() + 
  geom_text(model_effect_estimates_to_be_combined, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.8, y = 16:1), size = 3) + 
  geom_text(combined_ten_pd_df_to_be_combined, 
            mapping = aes(label = ci_95, x = 1.35, y = 16:1), size = 3) +
  xlim(-.8, 1.7) + 
  scale_y_continuous(breaks = 1:16, labels = combined_ten_pd_df_to_be_combined$term[16:1])

#set up var-cov for attributable deaths
sd_for_attr_deaths <- merge(model_effect_estimates[,c("estimate", "term")], 
                            combined_ten_pd_df[,c("term", "conf.low", "conf.high", "ci_95")],
                            by = "term")
rownames(sd_for_attr_deaths) <- sd_for_attr_deaths$term
bootstrap_attributable_deaths <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx, 
                                                    sd_for_attr_deaths, 
                                                    lin_model = TRUE, 
                                                    tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
bootstrap_attributable_deaths <- merge(bootstrap_attributable_deaths, date_data, by.x = "Time_Period", by.y = "Time_Period_ID")

bootstrap_attributable_deaths_summary <- bootstrap_attributable_deaths %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_lives_saved = sum(attr_deaths),
            total_lives_saved_lb = sum(attr_deaths_lb),
            total_lives_saved_ub = sum(attr_deaths_ub))

ggplot(bootstrap_attributable_deaths_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_lives_saved, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_lives_saved_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_lives_saved_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 
       Estimated Using Clustered Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

#plot with the observed od death
national_od_bs <- sensitivity_anlys_event_study_data_lin_post_tx %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_od = sum(imputed_deaths),
            total_od_prob = sum(imputed_deaths)/sum(population),
            total_pop = sum(population))

national_od_bs <- merge(national_od_bs, bootstrap_attributable_deaths_summary, by = "year")

ggplot(national_od_bs, aes(x = year)) + 
  geom_line(aes(y = total_lives_saved, linetype = "Estimate", color = "Lives Saved")) + 
  geom_line(aes(y = total_lives_saved_lb, linetype = "95% CI", color = "Lives Saved")) + 
  geom_line(aes(y = total_lives_saved_ub, linetype = "95% CI", color = "Lives Saved")) + 
  geom_line(aes(y = total_od, color = "Observed OD")) +
  geom_line(aes(y = total_od + total_lives_saved, color = "Potential Number of Deaths Had There Not Been DIH Prosecutions")) + 
  labs(x = "Year", y = "Number of People",
       title = "Number of Oberved OD, Estimated Number of Lives Saved,
       and Potential Number of OD Had There Not Been DIH Prosecutions 
       in Each Year, CI Estimated by Clustered Bootstrap",
       linetype = "", color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  guides(color=guide_legend(nrow=2,byrow=TRUE),
         linetype=guide_legend(nrow=2,byrow=TRUE))

6.7.3.2 Unit Level

bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit <- boostrap_state_time_unit(sensitivity_anlys_event_study_data_lin_post_tx,
                                                                  sensitivity_anlys_lin_post_tx_model_log_smoothed_time, 
                       coef_of_interest = rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,]))
colnames(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit) <-
  rownames(sensitivity_anlys_lin_post_tx_sd_and_ci_log_smoothed_time[51:66,])
mean_coef_outcome_lin_post_tx_unit <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit, 2, mean)
ci_coef_outcome_lin_post_tx_unit <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit, 2, quantile, probs = c(.025, .975))

coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit <- cbind(t(ci_coef_outcome_lin_post_tx_unit),
                                             mean_coef_outcome_lin_post_tx_unit)
colnames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit <- data.frame(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit$ci_95 <- paste("95% CI = (", 
                                                               format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit$conf.low,
                                                                            3), nsmall = 3),
                                                               ",",
                                                               format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit$conf.high,3), nsmall = 3),
                                                               ")", sep = "")
# write.csv(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit ,
#           "./Data/unit_bootstrap_smoothed_time_lin_tx_3_10_22.csv",
#           row.names = FALSE)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit <- read.csv("./Data/unit_bootstrap_smoothed_time_lin_tx_3_10_22.csv",
                                                            row.names = "term")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx)
dwplot(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit, colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient and 95% CI of Linear Post Tx Effects, 
       Smoothed Time Effects,
       Using Unit Bootstrap") + 
  scale_color_grey() + 
  geom_text(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit, 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

bootstrap_attributable_deaths_unit <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx, 
                                                    coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit, 
                                                    lin_model = TRUE, 
                                                    tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
bootstrap_attributable_deaths_unit <- merge(bootstrap_attributable_deaths_unit, date_data, by.x = "Time_Period", by.y = "Time_Period_ID")

bootstrap_attributable_deaths_unit <- bootstrap_attributable_deaths_unit %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(lives_saved = sum(attr_deaths),
            lives_saved_lb = sum(attr_deaths_lb),
            lives_saved_ub = sum(attr_deaths_ub))

ggplot(bootstrap_attributable_deaths_unit, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = lives_saved, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = lives_saved_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = lives_saved_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved per Year
       Using Model with Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 
       Estimated Using Unit Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.8 Analysis With Only Periods After Treatment Subset Periods

#run the gam model
data_subset <- sensitivity_anlys_event_study_data_lin_post_tx[sensitivity_anlys_event_study_data_lin_post_tx$Time_Period_ID <= 30,]
sensitivity_anlys_post_tx_model_log_smoothed_time_subset<-gam(log(prop_dead)~ State +
                                                                s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                                Naloxone_Pharmacy_Yes_Redefined + 
                                                                lag_num_pd_w_naloxone_yes +
                                                                Naloxone_Pharmacy_No_Redefined + 
                                                                lag_num_pd_w_naloxone_no +
                                                                Medical_Marijuana_Redefined + 
                                                                lag_num_pd_w_med_marijuana +
                                                                Recreational_Marijuana_Redefined + 
                                                                lag_num_pd_w_rec_marijuana +
                                                                GSL_Redefined + 
                                                                lag_num_pd_w_gsl +
                                                                PDMP_Redefined + 
                                                                lag_num_pd_w_pdmp +
                                                                Medicaid_Expansion_Redefined +
                                                                lag_num_pd_w_medicaid +
                                                                Intervention_Redefined +
                                                                lag_num_pd_w_tx,
                                                              data = data_subset)
summary(sensitivity_anlys_post_tx_model_log_smoothed_time_subset)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + Intervention_Redefined + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.954948   0.061205 -162.649  < 2e-16 ***
## StateAlaska                       0.381390   0.095290    4.002 6.59e-05 ***
## StateArizona                      0.389291   0.075663    5.145 3.05e-07 ***
## StateArkansas                    -0.446615   0.077142   -5.789 8.68e-09 ***
## StateCalifornia                   0.241295   0.100946    2.390 0.016963 *  
## StateColorado                     0.235636   0.091104    2.586 0.009796 ** 
## StateConnecticut                  0.473758   0.087270    5.429 6.67e-08 ***
## StateDelaware                     0.148283   0.077855    1.905 0.057033 .  
## StateFlorida                      0.566978   0.079812    7.104 1.91e-12 ***
## StateGeorgia                      0.312294   0.082129    3.802 0.000149 ***
## StateHawaii                      -0.308291   0.096742   -3.187 0.001470 ** 
## StateIdaho                       -0.246587   0.080690   -3.056 0.002285 ** 
## StateIllinois                     0.370402   0.085846    4.315 1.71e-05 ***
## StateIndiana                     -0.131136   0.079629   -1.647 0.099814 .  
## StateIowa                        -0.602878   0.074585   -8.083 1.34e-15 ***
## StateKansas                      -0.130069   0.074741   -1.740 0.082030 .  
## StateKentucky                     0.596009   0.080049    7.446 1.67e-13 ***
## StateLouisiana                    0.455849   0.074948    6.082 1.52e-09 ***
## StateMaine                        0.166905   0.093549    1.784 0.074615 .  
## StateMaryland                    -1.638096   0.081190  -20.176  < 2e-16 ***
## StateMassachusetts               -0.388948   0.080072   -4.857 1.32e-06 ***
## StateMichigan                    -0.035844   0.081616   -0.439 0.660603    
## StateMinnesota                   -0.525644   0.081890   -6.419 1.87e-10 ***
## StateMississippi                 -0.021348   0.074762   -0.286 0.775263    
## StateMissouri                     0.308129   0.079694    3.866 0.000115 ***
## StateMontana                     -0.086516   0.084766   -1.021 0.307596    
## StateNebraska                    -0.834823   0.077350  -10.793  < 2e-16 ***
## StateNevada                       0.691586   0.092387    7.486 1.24e-13 ***
## StateNew Hampshire                0.105377   0.077847    1.354 0.176066    
## StateNew Jersey                   0.189767   0.078171    2.428 0.015323 *  
## StateNew Mexico                   1.126814   0.098843   11.400  < 2e-16 ***
## StateNew York                    -0.111437   0.083562   -1.334 0.182556    
## StateNorth Carolina               0.298041   0.074578    3.996 6.76e-05 ***
## StateNorth Dakota                -1.266372   0.074452  -17.009  < 2e-16 ***
## StateOhio                         0.581803   0.079433    7.324 4.01e-13 ***
## StateOklahoma                     0.428696   0.079762    5.375 8.96e-08 ***
## StateOregon                       0.078047   0.094817    0.823 0.410569    
## StatePennsylvania                 0.625841   0.083619    7.484 1.26e-13 ***
## StateRhode Island                -0.595972   0.084992   -7.012 3.62e-12 ***
## StateSouth Carolina               0.176631   0.074971    2.356 0.018608 *  
## StateSouth Dakota                -1.061382   0.077016  -13.781  < 2e-16 ***
## StateTennessee                    0.415654   0.074607    5.571 3.02e-08 ***
## StateTexas                        0.138678   0.082925    1.672 0.094679 .  
## StateUtah                        -0.223429   0.079918   -2.796 0.005248 ** 
## StateVermont                     -0.094047   0.081078   -1.160 0.246260    
## StateVirginia                    -0.016319   0.076877   -0.212 0.831920    
## StateWashington                   0.371197   0.093388    3.975 7.40e-05 ***
## StateWest Virginia                0.604762   0.080240    7.537 8.54e-14 ***
## StateWisconsin                    0.100473   0.076983    1.305 0.192060    
## StateWyoming                     -0.018429   0.074503   -0.247 0.804669    
## Naloxone_Pharmacy_Yes_Redefined  -0.149963   0.075733   -1.980 0.047879 *  
## lag_num_pd_w_naloxone_yes        -0.076456   0.023347   -3.275 0.001083 ** 
## Naloxone_Pharmacy_No_Redefined    0.084187   0.053004    1.588 0.112440    
## lag_num_pd_w_naloxone_no         -0.042936   0.005548   -7.739 1.89e-14 ***
## Medical_Marijuana_Redefined       0.224811   0.039457    5.698 1.48e-08 ***
## lag_num_pd_w_med_marijuana       -0.016513   0.003072   -5.376 8.91e-08 ***
## Recreational_Marijuana_Redefined -0.242420   0.191420   -1.266 0.205567    
## lag_num_pd_w_rec_marijuana        0.007536   0.091363    0.082 0.934272    
## GSL_Redefined                     0.146337   0.055445    2.639 0.008399 ** 
## lag_num_pd_w_gsl                  0.016544   0.010296    1.607 0.108324    
## PDMP_Redefined                   -0.131089   0.029727   -4.410 1.11e-05 ***
## lag_num_pd_w_pdmp                 0.011944   0.002754    4.336 1.55e-05 ***
## Medicaid_Expansion_Redefined      0.078780   0.051045    1.543 0.122972    
## lag_num_pd_w_medicaid             0.007908   0.014802    0.534 0.593239    
## Intervention_Redefined           -0.018358   0.027678   -0.663 0.507271    
## lag_num_pd_w_tx                  -0.017760   0.002428   -7.314 4.33e-13 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   2.647  3.307 122.40  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 7.478  8.417  41.68  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     3.035  3.778  86.53  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      2.649  3.316  56.36  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.83   Deviance explained =   84%
## GCV = 0.085997  Scale est. = 0.081307  n = 1500

6.8.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset <-
  data.frame(predict(sensitivity_anlys_post_tx_model_log_smoothed_time_subset, type = "lpmatrix"))

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time_subset <- coef(sensitivity_anlys_post_tx_model_log_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset <- 
  compute_sd_and_CI(as.matrix(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset), 
                    log(data_subset$prop_dead),
                    coefficient_values_sensitivity_anlys_post_tx_log_smoothed_time_subset,
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_smoothed_time_subset) - 50)
# sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset

colnames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset) <- c("conf.low", "estimate", "conf.high", "sd")
sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset)

sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$ci_95 <- 
  paste("95% CI = (", format(round(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

6.8.2 Attributable Deaths

date_data_subset <- data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset <- date_data_subset[!duplicated(date_data_subset),]
attr_deaths_est_log_smoothed_time_lin_post_subset <- attr_death_compute(data_subset,
                                                                sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset, 
                                                        lin_model = TRUE, tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
attr_deaths_est_log_smoothed_time_lin_post_subset <- merge(attr_deaths_est_log_smoothed_time_lin_post_subset, date_data_subset, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")


attr_deaths_est_log_smoothed_time_lin_post_subset_summary <- attr_deaths_est_log_smoothed_time_lin_post_subset %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(total_lives_saved = sum(attr_deaths),
            total_lives_saved_lb = sum(attr_deaths_lb),
            total_lives_saved_ub = sum(attr_deaths_ub))

ggplot(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = total_lives_saved, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = total_lives_saved_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = total_lives_saved_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year 
       Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

ggplot() + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths, linetype = "Estimate",
                                                            color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_lb, linetype = "95% CI",
                                                            color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_summary, 
            mapping = aes(x = year, y = total_attr_deaths_ub, linetype = "95% CI",
                                                            color = "Full Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_lives_saved, linetype = "Estimate",
                                                            color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_lives_saved_lb, 
                                                                             linetype = "95% CI",
                                                            color = "Subset Dataset")) + 
  geom_line(attr_deaths_est_log_smoothed_time_lin_post_subset_summary, 
            mapping = aes(x = year, y = total_lives_saved_ub, 
                                                                             linetype = "95% CI",
                                                            color = "Subset Dataset")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "", color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.8.3 Bootstrapping: Coefficient and Attributable Death Estimate

6.8.3.1 Clustered

bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset <- boostrap_state_time_group(data_subset,
                                                                  sensitivity_anlys_post_tx_model_log_smoothed_time_subset, 
                       coef_of_interest = rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,]),
                       nSim = 5000)
colnames(bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset) <-
  rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,])
mean_coef_outcome_lin_post_tx_subset <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset, 2, mean)
ci_coef_outcome_lin_post_tx_subset <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset, 2, quantile, probs = c(.025, .975))

# write.csv(bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset,
#           "./Data/all_clustered_bootstrap_smoothed_time_lin_tx_subset_3_11_22_nSim_5000.csv",
#           row.names = FALSE)

ci_coef_outcome_lin_post_tx_subset <- cbind(t(ci_coef_outcome_lin_post_tx_subset),
                                             mean_coef_outcome_lin_post_tx_subset)
colnames(ci_coef_outcome_lin_post_tx_subset) <- c("conf.low", "conf.high", "estimate")
ci_coef_outcome_lin_post_tx_subset <- data.frame(ci_coef_outcome_lin_post_tx_subset)
ci_coef_outcome_lin_post_tx_subset$term <- rownames(ci_coef_outcome_lin_post_tx_subset)
ci_coef_outcome_lin_post_tx_subset$ci_95 <- paste("95% CI = (", 
                                                               format(round(ci_coef_outcome_lin_post_tx_subset$conf.low, 3), nsmall = 3),
                                                               ",",
                                                               format(round(ci_coef_outcome_lin_post_tx_subset$conf.high,3), nsmall = 3),
                                                               ")", sep = "")
# write.csv(ci_coef_outcome_lin_post_tx_subset,
#           "./Data/clustered_bootstrap_smoothed_time_lin_tx_subset_3_11_22_nSim_5000.csv",
#           row.names = FALSE)
bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset <-
  read.csv("./Data/all_clustered_bootstrap_smoothed_time_lin_tx_subset_3_11_22_nSim_5000.csv")


#first examine the distribution of coefficients
bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset_long <- bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset %>%
  pivot_longer(cols = everything(),
               names_to = "policy",
               values_to = "coef_est")

ggplot(bootstrap_smoothed_eff_log_outcome_lin_post_tx_subset_long, aes(y = coef_est, x = policy)) + 
  geom_boxplot() + 
  labs(x = "Policies", y = "Coefficient Estimates",
       title = "Clustered Bootstrap Distribution of Coefficient Estimates",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        axis.text.x = element_text(size = 5, angle = 45))

coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset <-
  read.csv("./Data/clustered_bootstrap_smoothed_time_lin_tx_subset_3_11_22_nSim_5000.csv",
                                                            row.names = "term")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset)


#multiply coefficients by 10 if they're the linear effects
ten_pd_eff_subset <- coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset[
  rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset) %in%
                                                                c("lag_num_pd_w_naloxone_yes",
                                                                  "lag_num_pd_w_naloxone_no",
                                                                  "lag_num_pd_w_med_marijuana",
                                                                  "lag_num_pd_w_rec_marijuana",
                                                                  "lag_num_pd_w_gsl",
                                                                  "lag_num_pd_w_pdmp",
                                                                  "lag_num_pd_w_medicaid",
                                                                  "lag_num_pd_w_tx"),]

ten_pd_eff_subset$estimate <- ten_pd_eff_subset$estimate*10
ten_pd_eff_subset$conf.high <- ten_pd_eff_subset$conf.high*10
ten_pd_eff_subset$conf.low <- ten_pd_eff_subset$conf.low*10
ten_pd_eff_subset$ci_95 <- paste("95% CI = (", format(round(ten_pd_eff_subset$conf.low, 3), nsmall = 3), ", ", 
                                 format(round(ten_pd_eff_subset$conf.high, 3), nsmall = 3), ")", sep = "")
rownames(ten_pd_eff_subset) <- ten_pd_eff_subset$term <- c("num_pd_w_naloxone_yes_per_5_yr",
                                             "num_pd_w_naloxone_no_per_5_yr",
                                             "num_pd_w_med_marijuana_per_5_yr",
                                             "num_pd_w_rec_marijuana_per_5_yr",
                                             "num_pd_w_gsl_per_5_yr",
                                             "num_pd_w_pdmp_per_5_yr",
                                             "num_pd_w_medicaid_per_5_yr",
                                             "num_pd_w_tx_per_5_yr")

combined_ten_pd_df_subset <- rbind(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_subset, ten_pd_eff_subset)
combined_ten_pd_df_subset_to_combine <- combined_ten_pd_df_subset %>%
  filter(!(term %in% c("lag_num_pd_w_naloxone_yes",
                       "lag_num_pd_w_naloxone_no",
                       "lag_num_pd_w_med_marijuana",
                       "lag_num_pd_w_rec_marijuana",
                       "lag_num_pd_w_gsl",
                       "lag_num_pd_w_pdmp",
                       "lag_num_pd_w_medicaid",
                       "lag_num_pd_w_tx")))


#put model estimates in the plot
model_effect_estimates_subset <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,]
model_effect_estimates_subset_to_combine <- model_effect_estimates_subset[
  !rownames(model_effect_estimates_subset) %in%
    c("lag_num_pd_w_naloxone_yes",
      "lag_num_pd_w_naloxone_no",
      "lag_num_pd_w_med_marijuana",
      "lag_num_pd_w_rec_marijuana",
      "lag_num_pd_w_gsl",
      "lag_num_pd_w_pdmp",
      "lag_num_pd_w_medicaid",
      "lag_num_pd_w_tx"),]
combined_ten_pd_model_est_subset <- sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[
  rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset) %in%
    c("lag_num_pd_w_naloxone_yes",
      "lag_num_pd_w_naloxone_no",
      "lag_num_pd_w_med_marijuana",
      "lag_num_pd_w_rec_marijuana",
      "lag_num_pd_w_gsl",
      "lag_num_pd_w_pdmp",
      "lag_num_pd_w_medicaid",
      "lag_num_pd_w_tx"),]
rownames(combined_ten_pd_model_est_subset) <- combined_ten_pd_model_est_subset$term <- c("num_pd_w_naloxone_yes_per_5_yr",
                                             "num_pd_w_naloxone_no_per_5_yr",
                                             "num_pd_w_med_marijuana_per_5_yr",
                                             "num_pd_w_rec_marijuana_per_5_yr",
                                             "num_pd_w_gsl_per_5_yr",
                                             "num_pd_w_pdmp_per_5_yr",
                                             "num_pd_w_medicaid_per_5_yr",
                                             "num_pd_w_tx_per_5_yr")
combined_ten_pd_model_est_subset$estimate <- combined_ten_pd_model_est_subset$estimate*10
combined_ten_pd_model_est_subset$conf.low <- combined_ten_pd_model_est_subset$conf.low*10
combined_ten_pd_model_est_subset$conf.high <- combined_ten_pd_model_est_subset$conf.high*10
combined_ten_pd_model_est_subset$ci_95 <- paste("95% CI = (", 
                                         format(round(combined_ten_pd_model_est_subset$conf.low, 3), nsmall = 3),
                                         ", ",
                                         format(round(combined_ten_pd_model_est_subset$conf.high, 3), nsmall = 3), ")", sep = "")

model_effect_estimates_subset_to_combine <- rbind(model_effect_estimates_subset_to_combine, combined_ten_pd_model_est_subset)


ggplot() +  
  geom_point(combined_ten_pd_df_subset_to_combine, mapping = aes(x = estimate, y = 16:1, color = "Bootstrap Estimate")) + 
  geom_linerange(combined_ten_pd_df_subset_to_combine, 
                 mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1, color = "Bootstrap Estimate")) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(color = "black"),
        legend.position = "bottom") +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  geom_point(model_effect_estimates_subset_to_combine, 
             mapping = aes(x = estimate, y = 16:1, color = "Model Estimate")) + 
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Model Coefficient and Bootstrap 95% CI of 
       Model Using Smoothed Time Effects, 
       Linear Policy Effects, 
       Using Clustered Bootstrap",
       color = "Estimate Type") + 
  # scale_color_grey() + 
  geom_text(model_effect_estimates_subset_to_combine, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 1.5, y = 16:1), size = 3) + 
  geom_text(combined_ten_pd_df_subset_to_combine, 
            mapping = aes(label = ci_95, x = 2.6, y = 16:1), size = 3) +
  xlim(-2, 3.3) + 
  scale_y_continuous(breaks = 1:16, labels = combined_ten_pd_df_subset_to_combine$term[16:1])

ggplot() +  
  # geom_point(combined_ten_pd_df_subset, mapping = aes(x = estimate, y = 16:1, color = "Bootstrap Estimate")) + 
  geom_linerange(combined_ten_pd_df_subset_to_combine, mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1)) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(color = "black"),
        legend.position = "bottom") +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  geom_point(model_effect_estimates_subset_to_combine, 
             mapping = aes(x = estimate, y = 16:1)) + 
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Model Coefficient and Bootstrap 95% CI of 
       Model Using Smoothed Time Effects, 
       Linear Policy Effects, 
       Using Clustered Bootstrap for Subset Data",
       color = "Estimate Type") + 
  # scale_color_grey() + 
  geom_text(model_effect_estimates_subset_to_combine, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 1.5, y = 16:1), size = 3) + 
  geom_text(combined_ten_pd_df_subset_to_combine, 
            mapping = aes(label = ci_95, x = 2.6, y = 16:1), size = 3) +
  xlim(-2, 3.3) + 
  scale_y_continuous(breaks = 1:16, labels = combined_ten_pd_df_subset_to_combine$term[16:1])

ggplot() +  
  # geom_point(combined_ten_pd_df_subset, mapping = aes(x = estimate, y = 16:1, color = "Bootstrap Estimate")) + 
  geom_linerange(combined_ten_pd_df_subset_to_combine, 
                 mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1, color = "Subset Data"),
                 alpha = 0.5) + 
  geom_linerange(model_effect_estimates_to_be_combined, 
                 mapping = aes(xmin = conf.low, xmax = conf.high, y = 16:1, color = "Full Data"), alpha = 0.5) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(color = "black"),
        legend.position = "bottom") +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  geom_point(model_effect_estimates_subset_to_combine, 
             mapping = aes(x = estimate, y = 16:1, color = "Subset Data")) + 
  geom_point(model_effect_estimates_to_be_combined,
             mapping = aes(x = estimate, y = 16:1, color = "Full Data")) + 
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Model Coefficient and Bootstrap 95% CI of 
       Model Using Smoothed Time Effects, 
       Linear Policy Effects, 
       Using Clustered Bootstrap for Full and Subset Data",
       color = "Estimate Type") + 
  # scale_color_grey() + 
  # geom_text(model_effect_estimates_subset, 
  #           mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 1.5, y = 16:1), size = 3) + 
  # geom_text(combined_ten_pd_df_subset, 
  #           mapping = aes(label = ci_95, x = 2.6, y = 16:1), size = 3) +
  # xlim(-2, 3.2) + 
  scale_y_continuous(breaks = 1:16, labels = combined_ten_pd_df_subset_to_combine$term[16:1])

sd_for_attr_death_subset <- merge(model_effect_estimates_subset[,c("term", "estimate")], 
                           combined_ten_pd_df_subset[,c("conf.low", "term", "conf.high", "ci_95")], 
                           by = "term")
rownames(sd_for_attr_death_subset) <- sd_for_attr_death_subset$term
bootstrap_attributable_deaths_subset <- attr_death_compute(data_subset, 
                                                    sd_for_attr_death_subset, 
                                                    lin_model = TRUE, 
                                                    tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
bootstrap_attributable_deaths_subset <- merge(bootstrap_attributable_deaths_subset, date_data_subset, 
                                              by.x = "Time_Period", by.y = "Time_Period_ID")

bootstrap_attributable_deaths_subset <- bootstrap_attributable_deaths_subset %>%
  group_by(year = year(Time_Period_Start)) %>%
  summarise(lives_saved = sum(attr_deaths),
            lives_saved_lb = sum(attr_deaths_lb),
            lives_saved_ub = sum(attr_deaths_ub))
ggplot(bootstrap_attributable_deaths_subset, aes(x = year)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = lives_saved, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = lives_saved_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = lives_saved_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year 
       Using Model with Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 
       Estimated Using Clustered Bootstrap on Subset Data",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

ggplot() + 
  geom_line(bootstrap_attributable_deaths_subset, 
            mapping = aes(x = year, y = lives_saved, linetype = "Estimate", color = "Subset Data")) + 
  geom_line(bootstrap_attributable_deaths_subset, 
             mapping = aes(x = year, y = lives_saved_lb, linetype = "95% CI", color = "Subset Data")) + 
  geom_line(bootstrap_attributable_deaths_subset, 
             mapping = aes(x = year, y = lives_saved_ub, linetype = "95% CI", color = "Subset Data")) + 
  geom_line(bootstrap_attributable_deaths_summary[bootstrap_attributable_deaths_summary$year <= 2014, ], 
             mapping = aes(x = year, y = total_lives_saved, linetype = "Estimate", color = "Full Data")) + 
  geom_line(bootstrap_attributable_deaths_summary[bootstrap_attributable_deaths_summary$year <= 2014, ], 
             mapping = aes(x = year, y = total_lives_saved_lb, linetype = "95% CI", color = "Full Data")) + 
  geom_line(bootstrap_attributable_deaths_summary[bootstrap_attributable_deaths_summary$year <= 2014, ], 
             mapping = aes(x = year, y = total_lives_saved_ub, linetype = "95% CI", color = "Full Data")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Per Year 
       Using Model with Smoothed Time Effects
       and Linear Policy Effects, 
       Estimated Using Clustered Bootstrap 
       for Subset Data and Full Data until 2015",
       linetype = "", color = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

6.8.3.2 Unit Level

bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset <- boostrap_state_time_unit(data_subset,
                                                                  sensitivity_anlys_post_tx_model_log_smoothed_time_subset, 
                       coef_of_interest = rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,]))
colnames(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset) <-
  rownames(sensitivity_anlys_post_tx_sd_and_ci_log_smoothed_time_subset[51:66,])
mean_coef_outcome_lin_post_tx_unit_subset <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset, 2, mean)
ci_coef_outcome_lin_post_tx_unit_subset <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset, 2, 
                                                 quantile, probs = c(.025, .975))
summary_coef_outcome_lin_post_tx_unit_subset <- apply(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset, 2, 
                                                 summary)
  
# write.csv(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset ,
#           "./Data/all_unit_bootstrap_smoothed_time_lin_tx_subset_3_10_22.csv",
#           row.names = FALSE)

coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset <- cbind(t(ci_coef_outcome_lin_post_tx_unit_subset),
                                             mean_coef_outcome_lin_post_tx_unit_subset)
colnames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset <- data.frame(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset)
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset$ci_95 <- paste("95% CI = (",
                                                                             format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset$conf.low,
                                                                     3), nsmall = 3),
                                                               ",",
                                                               format(round(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset$conf.high,3), nsmall = 3),
                                                               ")", sep = "")
# write.csv(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset ,
#           "./Data/unit_bootstrap_smoothed_time_lin_tx_subset_3_10_22.csv",
#           row.names = FALSE)
bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset <- read.csv("./Data/all_unit_bootstrap_smoothed_time_lin_tx_subset_3_10_22.csv")

#first examine the disribution of coefficients
bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset_long <- bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset %>%
  pivot_longer(cols = everything(),
               names_to = "policy",
               values_to = "coef_est")

ggplot(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset_long, aes(y = coef_est, x = policy)) + 
  geom_boxplot() + 
  labs(x = "Policies", y = "Coefficient Estimates",
       title = "Bootstrap Distribution of Coefficient Estimates",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        axis.text.x = element_text(size = 5, angle = 45))

ggplot(bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset_long[
  !bootstrap_smoothed_eff_log_outcome_lin_post_tx_unit_subset_long$policy %in% 
    c("Recreational_Marijuana_Redefined", "lag_num_pd_w_rec_marijuana"),], aes(y = coef_est, x = policy)) + 
  geom_boxplot() + 
  labs(x = "Policies", y = "Coefficient Estimates",
       title = "Bootstrap Distribution of Coefficient Estimates Without Recreational Marijuana Policies",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        axis.text.x = element_text(size = 5, angle = 45))

coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset <- read.csv("./Data/unit_bootstrap_smoothed_time_lin_tx_subset_3_10_22.csv",
                                                            row.names = "term")
coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset$term <- rownames(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset)
dwplot(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset, colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient and 95% CI of Linear Post Tx Effects, 
       Smoothed Time Effects,
       Using Unit Bootstrap, Subset Data") + 
  scale_color_grey() + 
  geom_text(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset, 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset, 
            mapping = aes(label = ci_95, x = 1, y = 16:1), size = 3) +
  xlim(-.65, 1.2)

bootstrap_attributable_deaths_unit_subset <- attr_death_compute(data_subset, 
                                                    coef_w_ci_smoothed_time_log_outcome_lin_post_tx_unit_subset, 
                                                    lin_model = TRUE, 
                                                    tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
bootstrap_attributable_deaths_unit_subset <- merge(bootstrap_attributable_deaths_unit_subset, date_data_subset, 
                                                   by.x = "Time_Period", by.y = "Time_Period_ID")
ggplot(bootstrap_attributable_deaths_unit_subset, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 
       Estimated Using Unit Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7 OLS Model Main Analysis With Linear Fixed Time Effects Interacted with Region With Log Proportion

7.1 Main Model

#compute the proportion of people who died from drug overdose
main_analysis_data$prop_dead <- main_analysis_data$imputed_deaths/main_analysis_data$population


#fit an OLS with smoothed time effects
main_analysis_model_log_fixed_lin_time<-lm(log(prop_dead)~ State +
                                         Time_Period_ID:Region +
                                         I(Time_Period_ID^2):Region + 
                                         Naloxone_Pharmacy_Yes_Redefined +
                                         Naloxone_Pharmacy_No_Redefined +
                                         Medical_Marijuana_Redefined +
                                         Recreational_Marijuana_Redefined +
                                         GSL_Redefined +
                                         PDMP_Redefined +
                                         Medicaid_Expansion_Redefined +
                                         Intervention_Redefined ,
                                       data = main_analysis_data)

summary(main_analysis_model_log_fixed_lin_time)
## 
## Call:
## lm(formula = log(prop_dead) ~ State + Time_Period_ID:Region + 
##     I(Time_Period_ID^2):Region + Naloxone_Pharmacy_Yes_Redefined + 
##     Naloxone_Pharmacy_No_Redefined + Medical_Marijuana_Redefined + 
##     Recreational_Marijuana_Redefined + GSL_Redefined + PDMP_Redefined + 
##     Medicaid_Expansion_Redefined + Intervention_Redefined, data = main_analysis_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.11885 -0.14274  0.01294  0.16151  1.08925 
## 
## Coefficients:
##                                       Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                         -1.077e+01  5.984e-02 -180.045  < 2e-16 ***
## StateAlaska                          3.765e-01  9.020e-02    4.174 3.13e-05 ***
## StateArizona                         5.260e-01  8.687e-02    6.055 1.69e-09 ***
## StateArkansas                       -4.947e-01  6.844e-02   -7.228 7.02e-13 ***
## StateCalifornia                      6.715e-02  9.010e-02    0.745 0.456170    
## StateColorado                        2.472e-01  9.004e-02    2.745 0.006106 ** 
## StateConnecticut                     9.452e-02  9.118e-02    1.037 0.300018    
## StateDelaware                        1.911e-01  6.967e-02    2.743 0.006149 ** 
## StateFlorida                         3.201e-01  6.849e-02    4.674 3.15e-06 ***
## StateGeorgia                         7.540e-04  6.848e-02    0.011 0.991216    
## StateHawaii                         -1.573e-01  9.011e-02   -1.746 0.080971 .  
## StateIdaho                           1.127e-01  8.783e-02    1.283 0.199685    
## StateIllinois                        1.101e-02  8.772e-02    0.126 0.900137    
## StateIndiana                        -3.875e-02  8.750e-02   -0.443 0.657902    
## StateIowa                           -7.871e-01  8.696e-02   -9.052  < 2e-16 ***
## StateKansas                         -3.434e-01  8.680e-02   -3.957 7.87e-05 ***
## StateKentucky                        6.920e-01  6.845e-02   10.109  < 2e-16 ***
## StateLouisiana                       3.491e-01  6.767e-02    5.159 2.73e-07 ***
## StateMaine                          -6.228e-02  9.377e-02   -0.664 0.506700    
## StateMaryland                       -1.509e+00  6.945e-02  -21.732  < 2e-16 ***
## StateMassachusetts                  -2.204e-01  9.110e-02   -2.420 0.015632 *  
## StateMichigan                       -1.065e-01  8.789e-02   -1.212 0.225640    
## StateMinnesota                      -7.849e-01  8.880e-02   -8.838  < 2e-16 ***
## StateMississippi                    -5.743e-02  6.772e-02   -0.848 0.396496    
## StateMissouri                        4.478e-02  8.831e-02    0.507 0.612120    
## StateMontana                        -1.949e-01  8.772e-02   -2.222 0.026399 *  
## StateNebraska                       -9.822e-01  8.789e-02  -11.175  < 2e-16 ***
## StateNevada                          6.930e-01  8.880e-02    7.804 9.72e-15 ***
## StateNew Hampshire                   1.089e-02  9.090e-02    0.120 0.904628    
## StateNew Jersey                     -5.519e-02  9.043e-02   -0.610 0.541703    
## StateNew Mexico                      8.623e-01  8.919e-02    9.668  < 2e-16 ***
## StateNew York                       -2.743e-01  9.079e-02   -3.022 0.002547 ** 
## StateNorth Carolina                  2.243e-01  6.753e-02    3.321 0.000915 ***
## StateNorth Dakota                   -1.267e+00  8.728e-02  -14.511  < 2e-16 ***
## StateOhio                            3.399e-01  8.691e-02    3.911 9.52e-05 ***
## StateOklahoma                        4.557e-01  6.816e-02    6.686 2.98e-11 ***
## StateOregon                         -6.969e-02  8.992e-02   -0.775 0.438394    
## StatePennsylvania                    4.449e-01  9.126e-02    4.875 1.18e-06 ***
## StateRhode Island                   -4.293e-01  9.162e-02   -4.686 2.98e-06 ***
## StateSouth Carolina                  2.013e-01  6.809e-02    2.957 0.003148 ** 
## StateSouth Dakota                   -1.160e+00  8.796e-02  -13.187  < 2e-16 ***
## StateTennessee                       4.640e-01  6.738e-02    6.887 7.67e-12 ***
## StateTexas                          -6.816e-03  6.842e-02   -0.100 0.920656    
## StateUtah                            1.602e-01  8.655e-02    1.851 0.064293 .  
## StateVermont                        -3.249e-01  9.116e-02   -3.564 0.000374 ***
## StateVirginia                       -2.543e-02  6.768e-02   -0.376 0.707139    
## StateWashington                      2.596e-01  9.036e-02    2.873 0.004115 ** 
## StateWest Virginia                   7.942e-01  6.850e-02   11.595  < 2e-16 ***
## StateWisconsin                      -1.123e-01  8.698e-02   -1.291 0.197017    
## StateWyoming                         2.347e-01  8.695e-02    2.699 0.007005 ** 
## Naloxone_Pharmacy_Yes_Redefined      5.414e-02  3.941e-02    1.374 0.169687    
## Naloxone_Pharmacy_No_Redefined       1.445e-02  3.883e-02    0.372 0.709784    
## Medical_Marijuana_Redefined          2.141e-01  3.118e-02    6.868 8.73e-12 ***
## Recreational_Marijuana_Redefined    -8.103e-02  4.907e-02   -1.651 0.098804 .  
## GSL_Redefined                        4.745e-02  3.175e-02    1.495 0.135166    
## PDMP_Redefined                      -1.653e-01  2.496e-02   -6.623 4.55e-11 ***
## Medicaid_Expansion_Redefined         7.859e-02  3.003e-02    2.617 0.008939 ** 
## Intervention_Redefined              -5.397e-02  2.454e-02   -2.199 0.027986 *  
## Time_Period_ID:RegionMidwest         8.179e-02  5.292e-03   15.456  < 2e-16 ***
## Time_Period_ID:RegionNortheast       7.251e-02  6.096e-03   11.896  < 2e-16 ***
## Time_Period_ID:RegionSouth           7.480e-02  4.671e-03   16.015  < 2e-16 ***
## Time_Period_ID:RegionWest            6.411e-02  5.191e-03   12.349  < 2e-16 ***
## RegionMidwest:I(Time_Period_ID^2)   -8.933e-04  1.294e-04   -6.901 6.98e-12 ***
## RegionNortheast:I(Time_Period_ID^2) -5.466e-04  1.495e-04   -3.657 0.000262 ***
## RegionSouth:I(Time_Period_ID^2)     -8.581e-04  1.189e-04   -7.217 7.61e-13 ***
## RegionWest:I(Time_Period_ID^2)      -9.039e-04  1.305e-04   -6.927 5.83e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.3009 on 1934 degrees of freedom
## Multiple R-squared:  0.8371, Adjusted R-squared:  0.8316 
## F-statistic: 152.9 on 65 and 1934 DF,  p-value: < 2.2e-16
#examine fitted values
summary(fitted(main_analysis_model_log_fixed_lin_time))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -12.209 -10.218  -9.750  -9.796  -9.329  -8.127
hist(fitted(main_analysis_model_log_fixed_lin_time))

par(mfrow = c(2,2))
plot(main_analysis_model_log_fixed_lin_time)

7.1.1 Coefficients and 95% CI

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time <- model.matrix(main_analysis_model_log_fixed_lin_time)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time <- coef(main_analysis_model_log_fixed_lin_time)
#type = "response" to get the estimated probabilities
main_analysis_sd_and_ci_log_fixed_lin_time <- compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time,
                                                                log(main_analysis_data$prop_dead),
                                             coefficient_values_log_fixed_lin_time,
                                             p = ncol(full_df_w_basis_functions_log_fixed_lin_time) - 50)
# main_analysis_sd_and_ci_log_fixed_lin_time

colnames(main_analysis_sd_and_ci_log_fixed_lin_time) <- c("conf.low", "estimate", "conf.high", "sd")
main_analysis_sd_and_ci_log_fixed_lin_time$term <- rownames(main_analysis_sd_and_ci_log_fixed_lin_time)


main_analysis_sd_and_ci_log_fixed_lin_time$ci_95 <- 
  paste("95% CI = (", format(round(main_analysis_sd_and_ci_log_fixed_lin_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(main_analysis_sd_and_ci_log_fixed_lin_time$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(main_analysis_sd_and_ci_log_fixed_lin_time[51:58,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Linear/Quad Time Effects, 
       Exposure Intervention") + 
  scale_color_grey()     + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time[51:58,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 8:1), size = 3) + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time[51:58,], 
            mapping = aes(label = ci_95, x = 0.9, y = 8:1), size = 3) +
  xlim(-.3, 1.1)

7.1.2 Attributable Deaths

date_data <- main_analysis_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_lin_time <- attr_death_compute(main_analysis_data,
                                                   main_analysis_sd_and_ci_log_fixed_lin_time, 
                                                   lin_model = TRUE, tx_name = "Intervention_Redefined")
attr_deaths_est_log_lin_time <- merge(attr_deaths_est_log_lin_time, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear and Quad. Time Effects, 
       Log Probability of Drug Overdose Death",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.1.3 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_lin_eff_log_outcome <- boostrap_state_time(main_analysis_data,
                                                     main_analysis_model_log_fixed_lin_time,
                                                     lin_model = TRUE, 
                                                     tx_name = "Intervention_Redefined")

reduced_coef_outcome_lin_eff_log_outcome <- Reduce("cbind", bootstrap_lin_eff_log_outcome[[1]])

coef_w_ci_lin_eff_log_outcome <- cbind(t(apply(reduced_coef_outcome_lin_eff_log_outcome, 1, quantile, 
                                                                 probs = c(.025, .975))),
                                             apply(reduced_coef_outcome_lin_eff_log_outcome, 1, mean))
colnames(coef_w_ci_lin_eff_log_outcome) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_lin_eff_log_outcome <- data.frame(coef_w_ci_lin_eff_log_outcome)
coef_w_ci_lin_eff_log_outcome$term <- rownames(coef_w_ci_lin_eff_log_outcome)

dwplot(coef_w_ci_lin_eff_log_outcome[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient and 95% CI of Linear Post Tx Effects Subset, 
       Using Bootstrap") + 
  scale_color_grey() 

list_attr_deaths_lin_eff_log_outcome <- lapply(bootstrap_lin_eff_log_outcome[[2]], function(x){x$attr_deaths})
reduced_attr_deaths_lin_eff_log_outcome <- Reduce("cbind", list_attr_deaths_lin_eff_log_outcome)
attr_deaths_lin_eff_log_outcome <- cbind(t(apply(reduced_attr_deaths_lin_eff_log_outcome, 1, quantile, 
                                                       probs = c(.025, .975), na.rm = TRUE)), 
                                               apply(reduced_attr_deaths_lin_eff_log_outcome, 1, mean, na.rm = TRUE))
colnames(attr_deaths_lin_eff_log_outcome) <- c("conf.low", "conf.high", "estimate")
attr_deaths_lin_eff_log_outcome <- data.frame(attr_deaths_lin_eff_log_outcome)
attr_deaths_lin_eff_log_outcome$Time_Period_Start <- unique(main_analysis_data$Time_Period_Start)

ggplot(attr_deaths_lin_eff_log_outcome, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.2 Linear Policy Measures Post Tx

#run the gam model
sensitivity_anlys_lin_post_tx_model_linear_time<-lm(log(prop_dead)~ State +
                                                      Time_Period_ID:Region  +
                                                      I(Time_Period_ID^2):Region +
                                                      Naloxone_Pharmacy_Yes_Redefined + 
                                                      lag_num_pd_w_naloxone_yes +
                                                      Naloxone_Pharmacy_No_Redefined + 
                                                      lag_num_pd_w_naloxone_no +
                                                      Medical_Marijuana_Redefined + 
                                                      lag_num_pd_w_med_marijuana +
                                                      Recreational_Marijuana_Redefined + 
                                                      lag_num_pd_w_rec_marijuana +
                                                      GSL_Redefined + 
                                                      lag_num_pd_w_gsl +
                                                      PDMP_Redefined + 
                                                      lag_num_pd_w_pdmp +
                                                      Medicaid_Expansion_Redefined +
                                                      lag_num_pd_w_medicaid +
                                                      Intervention_Redefined +
                                                      lag_num_pd_w_tx,
                                                    data = sensitivity_anlys_event_study_data_lin_post_tx)
summary(sensitivity_anlys_lin_post_tx_model_linear_time)
## 
## Call:
## lm(formula = log(prop_dead) ~ State + Time_Period_ID:Region + 
##     I(Time_Period_ID^2):Region + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + Intervention_Redefined + lag_num_pd_w_tx, 
##     data = sensitivity_anlys_event_study_data_lin_post_tx)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.09592 -0.13585  0.01351  0.16008  1.07518 
## 
## Coefficients:
##                                       Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                         -1.080e+01  5.852e-02 -184.538  < 2e-16 ***
## StateAlaska                          3.228e-01  9.027e-02    3.576 0.000357 ***
## StateArizona                         4.144e-01  8.859e-02    4.678 3.10e-06 ***
## StateArkansas                       -5.341e-01  6.970e-02   -7.662 2.87e-14 ***
## StateCalifornia                      2.405e-01  9.431e-02    2.551 0.010833 *  
## StateColorado                        2.424e-01  8.953e-02    2.708 0.006828 ** 
## StateConnecticut                     5.159e-02  9.230e-02    0.559 0.576253    
## StateDelaware                        1.252e-01  7.090e-02    1.766 0.077544 .  
## StateFlorida                         5.019e-01  7.107e-02    7.062 2.29e-12 ***
## StateGeorgia                         2.439e-01  7.316e-02    3.333 0.000874 ***
## StateHawaii                         -2.286e-01  9.240e-02   -2.474 0.013437 *  
## StateIdaho                          -1.064e-01  9.296e-02   -1.145 0.252319    
## StateIllinois                        8.783e-02  9.287e-02    0.946 0.344444    
## StateIndiana                        -1.790e-01  9.031e-02   -1.982 0.047599 *  
## StateIowa                           -7.549e-01  8.499e-02   -8.882  < 2e-16 ***
## StateKansas                         -3.503e-01  8.448e-02   -4.146 3.53e-05 ***
## StateKentucky                        5.812e-01  7.133e-02    8.148 6.58e-16 ***
## StateLouisiana                       4.410e-01  6.654e-02    6.628 4.41e-11 ***
## StateMaine                           5.389e-02  9.619e-02    0.560 0.575386    
## StateMaryland                       -1.400e+00  7.288e-02  -19.208  < 2e-16 ***
## StateMassachusetts                  -3.516e-01  9.232e-02   -3.809 0.000144 ***
## StateMichigan                       -9.408e-02  9.123e-02   -1.031 0.302574    
## StateMinnesota                      -7.379e-01  8.836e-02   -8.352  < 2e-16 ***
## StateMississippi                    -1.510e-01  6.640e-02   -2.275 0.023024 *  
## StateMissouri                        1.257e-01  9.043e-02    1.390 0.164611    
## StateMontana                        -3.138e-02  9.008e-02   -0.348 0.727587    
## StateNebraska                       -1.072e+00  8.719e-02  -12.298  < 2e-16 ***
## StateNevada                          7.656e-01  9.098e-02    8.415  < 2e-16 ***
## StateNew Hampshire                  -4.597e-02  9.278e-02   -0.495 0.620312    
## StateNew Jersey                      2.552e-02  9.132e-02    0.279 0.779962    
## StateNew Mexico                      8.084e-01  9.174e-02    8.812  < 2e-16 ***
## StateNew York                       -3.869e-01  9.164e-02   -4.222 2.53e-05 ***
## StateNorth Carolina                  2.905e-01  6.608e-02    4.396 1.16e-05 ***
## StateNorth Dakota                   -1.392e+00  8.546e-02  -16.290  < 2e-16 ***
## StateOhio                            4.977e-01  8.750e-02    5.688 1.48e-08 ***
## StateOklahoma                        3.746e-01  7.097e-02    5.279 1.45e-07 ***
## StateOregon                          4.102e-02  9.032e-02    0.454 0.649769    
## StatePennsylvania                    5.119e-01  9.483e-02    5.398 7.59e-08 ***
## StateRhode Island                   -5.460e-01  9.267e-02   -5.892 4.50e-09 ***
## StateSouth Carolina                  1.072e-01  6.713e-02    1.596 0.110620    
## StateSouth Dakota                   -1.276e+00  8.721e-02  -14.632  < 2e-16 ***
## StateTennessee                       4.546e-01  6.605e-02    6.882 7.98e-12 ***
## StateTexas                           1.302e-01  7.340e-02    1.773 0.076340 .  
## StateUtah                            7.212e-02  9.213e-02    0.783 0.433846    
## StateVermont                        -3.089e-01  8.940e-02   -3.455 0.000563 ***
## StateVirginia                        5.348e-02  6.798e-02    0.787 0.431585    
## StateWashington                      2.819e-01  9.011e-02    3.128 0.001787 ** 
## StateWest Virginia                   6.519e-01  7.155e-02    9.110  < 2e-16 ***
## StateWisconsin                      -5.148e-02  8.564e-02   -0.601 0.547801    
## StateWyoming                         1.131e-01  8.923e-02    1.268 0.204922    
## Naloxone_Pharmacy_Yes_Redefined      6.687e-02  3.899e-02    1.715 0.086445 .  
## lag_num_pd_w_naloxone_yes           -1.264e-02  5.921e-03   -2.135 0.032850 *  
## Naloxone_Pharmacy_No_Redefined       1.414e-01  4.175e-02    3.386 0.000723 ***
## lag_num_pd_w_naloxone_no            -2.092e-02  4.089e-03   -5.117 3.42e-07 ***
## Medical_Marijuana_Redefined          2.019e-01  3.093e-02    6.528 8.49e-11 ***
## lag_num_pd_w_med_marijuana          -1.018e-02  2.228e-03   -4.568 5.22e-06 ***
## Recreational_Marijuana_Redefined    -9.838e-03  6.186e-02   -0.159 0.873659    
## lag_num_pd_w_rec_marijuana          -1.003e-02  1.092e-02   -0.918 0.358520    
## GSL_Redefined                        5.183e-02  3.310e-02    1.566 0.117574    
## lag_num_pd_w_gsl                     1.398e-02  4.144e-03    3.374 0.000757 ***
## PDMP_Redefined                      -1.484e-01  2.759e-02   -5.380 8.37e-08 ***
## lag_num_pd_w_pdmp                    4.282e-03  2.400e-03    1.784 0.074577 .  
## Medicaid_Expansion_Redefined         5.333e-02  3.321e-02    1.606 0.108441    
## lag_num_pd_w_medicaid                1.663e-02  5.017e-03    3.314 0.000938 ***
## Intervention_Redefined              -7.310e-02  2.430e-02   -3.009 0.002658 ** 
## lag_num_pd_w_tx                     -1.632e-02  1.811e-03   -9.015  < 2e-16 ***
## Time_Period_ID:RegionMidwest         8.585e-02  5.287e-03   16.238  < 2e-16 ***
## Time_Period_ID:RegionNortheast       7.830e-02  6.271e-03   12.487  < 2e-16 ***
## Time_Period_ID:RegionSouth           7.640e-02  4.873e-03   15.678  < 2e-16 ***
## Time_Period_ID:RegionWest            7.354e-02  5.657e-03   13.000  < 2e-16 ***
## RegionMidwest:I(Time_Period_ID^2)   -8.227e-04  1.346e-04   -6.111 1.20e-09 ***
## RegionNortheast:I(Time_Period_ID^2) -4.923e-04  1.572e-04   -3.132 0.001762 ** 
## RegionSouth:I(Time_Period_ID^2)     -7.710e-04  1.291e-04   -5.973 2.76e-09 ***
## RegionWest:I(Time_Period_ID^2)      -9.489e-04  1.382e-04   -6.865 8.94e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2911 on 1926 degrees of freedom
## Multiple R-squared:  0.8481, Adjusted R-squared:  0.8423 
## F-statistic: 147.3 on 73 and 1926 DF,  p-value: < 2.2e-16

7.2.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx <- model.matrix(sensitivity_anlys_lin_post_tx_model_linear_time)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx <- coef(sensitivity_anlys_lin_post_tx_model_linear_time)
#type = "response" to get the estimated probabilities
main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx <- compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx,
                                                                log(sensitivity_anlys_event_study_data_lin_post_tx$prop_dead),
                                             coefficient_values_log_fixed_lin_time_lin_post_tx,
                                             p = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx) - 50)
# format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx,4)

colnames(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx) <- c("conf.low", "estimate", "conf.high", "sd")
main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term <- rownames(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx)




main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$ci_95 <- 
  paste("95% CI = (", format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low, 3), nsmall = 3), ", ", 
        format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Linear/Quad Time Effects, 
       Linear Intervention") + 
  scale_color_grey()      + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.3, 1.1)

7.2.2 Attributable Deaths

date_data <- main_analysis_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_lin_time_lin_post_tx <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx,
                                                   main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx, 
                                                   lin_model = TRUE, tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
attr_deaths_est_log_lin_time_lin_post_tx <- merge(attr_deaths_est_log_lin_time_lin_post_tx, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_lin_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear and Quad. Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.2.3 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_lin_eff_log_outcome_lin_post_tx <- boostrap_state_time(sensitivity_anlys_event_study_data_lin_post_tx,
                                                     sensitivity_anlys_lin_post_tx_model_linear_time,
                                                     lin_model = TRUE, 
                                                     tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))

reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx  <- Reduce("cbind", bootstrap_lin_eff_log_outcome_lin_post_tx [[1]])

coef_w_ci_lin_eff_log_outcome_lin_post_tx  <- cbind(t(apply(reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx , 1, quantile, 
                                                                 probs = c(.025, .975))),
                                             apply(reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx , 1, mean))
colnames(coef_w_ci_lin_eff_log_outcome_lin_post_tx ) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_lin_eff_log_outcome_lin_post_tx  <- data.frame(coef_w_ci_lin_eff_log_outcome_lin_post_tx )
coef_w_ci_lin_eff_log_outcome_lin_post_tx $term <- rownames(coef_w_ci_lin_eff_log_outcome_lin_post_tx )

dwplot(coef_w_ci_lin_eff_log_outcome_lin_post_tx [51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient and 95% CI of Linear Post Tx Effects Subset, 
       Using Bootstrap") + 
  scale_color_grey() 

list_attr_deaths_lin_eff_log_outcome_lin_post_tx  <- lapply(bootstrap_lin_eff_log_outcome_lin_post_tx [[2]], function(x){x$attr_deaths})
reduced_attr_deaths_lin_eff_log_outcome_lin_post_tx  <- Reduce("cbind", list_attr_deaths_lin_eff_log_outcome_lin_post_tx )
attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx  <- cbind(t(apply(reduced_attr_deaths_lin_eff_log_outcome_lin_post_tx,  1,
                                                                            quantile, 
                                                       probs = c(.025, .975), na.rm = TRUE)), 
                                               apply(reduced_attr_deaths_lin_eff_log_outcome, 1, mean, na.rm = TRUE))
colnames(attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx ) <- c("conf.low", "conf.high", "estimate")
attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx <- data.frame(attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx)
attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx$Time_Period_Start <- unique(main_analysis_data$Time_Period_Start)

ggplot(attr_deaths_smoothed_time_lin_eff_log_outcome_lin_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.3 Linear Policy Measures Post Tx Subset

#run the gam model
sensitivity_anlys_lin_post_tx_model_linear_time_subset<-lm(log(prop_dead)~ State +
                                                      Time_Period_ID:Region  +
                                                      I(Time_Period_ID^2):Region +
                                                      Naloxone_Pharmacy_Yes_Redefined + 
                                                      lag_num_pd_w_naloxone_yes +
                                                      Naloxone_Pharmacy_No_Redefined + 
                                                      lag_num_pd_w_naloxone_no +
                                                      Medical_Marijuana_Redefined + 
                                                      lag_num_pd_w_med_marijuana +
                                                      Recreational_Marijuana_Redefined + 
                                                      lag_num_pd_w_rec_marijuana +
                                                      GSL_Redefined + 
                                                      lag_num_pd_w_gsl +
                                                      PDMP_Redefined + 
                                                      lag_num_pd_w_pdmp +
                                                      Medicaid_Expansion_Redefined +
                                                      lag_num_pd_w_medicaid +
                                                      Intervention_Redefined +
                                                      lag_num_pd_w_tx,
                                                    data = data_subset)
summary(sensitivity_anlys_lin_post_tx_model_linear_time_subset)
## 
## Call:
## lm(formula = log(prop_dead) ~ State + Time_Period_ID:Region + 
##     I(Time_Period_ID^2):Region + Naloxone_Pharmacy_Yes_Redefined + 
##     lag_num_pd_w_naloxone_yes + Naloxone_Pharmacy_No_Redefined + 
##     lag_num_pd_w_naloxone_no + Medical_Marijuana_Redefined + 
##     lag_num_pd_w_med_marijuana + Recreational_Marijuana_Redefined + 
##     lag_num_pd_w_rec_marijuana + GSL_Redefined + lag_num_pd_w_gsl + 
##     PDMP_Redefined + lag_num_pd_w_pdmp + Medicaid_Expansion_Redefined + 
##     lag_num_pd_w_medicaid + Intervention_Redefined + lag_num_pd_w_tx, 
##     data = data_subset)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.97459 -0.13375  0.01189  0.14845  1.01555 
## 
## Coefficients:
##                                       Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                         -1.089e+01  6.671e-02 -163.304  < 2e-16 ***
## StateAlaska                          4.501e-01  1.039e-01    4.332 1.58e-05 ***
## StateArizona                         4.736e-01  1.004e-01    4.716 2.64e-06 ***
## StateArkansas                       -4.426e-01  7.804e-02   -5.672 1.71e-08 ***
## StateCalifornia                      3.002e-01  1.093e-01    2.747 0.006099 ** 
## StateColorado                        3.102e-01  1.019e-01    3.045 0.002371 ** 
## StateConnecticut                     3.033e-01  1.071e-01    2.833 0.004680 ** 
## StateDelaware                        1.424e-01  7.871e-02    1.809 0.070731 .  
## StateFlorida                         5.745e-01  8.071e-02    7.118 1.73e-12 ***
## StateGeorgia                         3.217e-01  8.305e-02    3.873 0.000112 ***
## StateHawaii                         -2.445e-01  1.054e-01   -2.319 0.020510 *  
## StateIdaho                          -1.751e-01  1.056e-01   -1.658 0.097490 .  
## StateIllinois                        2.490e-01  1.081e-01    2.303 0.021402 *  
## StateIndiana                        -2.476e-01  1.038e-01   -2.385 0.017198 *  
## StateIowa                           -7.029e-01  9.738e-02   -7.218 8.56e-13 ***
## StateKansas                         -2.274e-01  9.706e-02   -2.343 0.019291 *  
## StateKentucky                        5.756e-01  8.103e-02    7.103 1.91e-12 ***
## StateLouisiana                       4.600e-01  7.582e-02    6.067 1.67e-09 ***
## StateMaine                          -4.609e-03  1.102e-01   -0.042 0.966643    
## StateMaryland                       -1.641e+00  8.201e-02  -20.015  < 2e-16 ***
## StateMassachusetts                  -5.710e-01  1.058e-01   -5.395 8.02e-08 ***
## StateMichigan                       -1.554e-01  1.042e-01   -1.491 0.136199    
## StateMinnesota                      -6.418e-01  1.014e-01   -6.327 3.33e-10 ***
## StateMississippi                    -2.782e-02  7.563e-02   -0.368 0.713009    
## StateMissouri                        2.182e-01  9.989e-02    2.185 0.029063 *  
## StateMontana                         7.298e-03  1.025e-01    0.071 0.943250    
## StateNebraska                       -9.332e-01  9.888e-02   -9.438  < 2e-16 ***
## StateNevada                          7.628e-01  1.044e-01    7.306 4.57e-13 ***
## StateNew Hampshire                  -4.874e-02  1.053e-01   -0.463 0.643452    
## StateNew Jersey                      2.156e-02  1.058e-01    0.204 0.838597    
## StateNew Mexico                      1.193e+00  1.109e-01   10.763  < 2e-16 ***
## StateNew York                       -2.930e-01  1.057e-01   -2.773 0.005624 ** 
## StateNorth Carolina                  2.942e-01  7.545e-02    3.899 0.000101 ***
## StateNorth Dakota                   -1.373e+00  9.784e-02  -14.038  < 2e-16 ***
## StateOhio                            4.800e-01  1.014e-01    4.733 2.43e-06 ***
## StateOklahoma                        4.126e-01  8.073e-02    5.111 3.64e-07 ***
## StateOregon                          1.610e-01  1.033e-01    1.559 0.119277    
## StatePennsylvania                    4.573e-01  1.101e-01    4.153 3.47e-05 ***
## StateRhode Island                   -7.885e-01  1.066e-01   -7.396 2.39e-13 ***
## StateSouth Carolina                  1.723e-01  7.584e-02    2.272 0.023234 *  
## StateSouth Dakota                   -1.163e+00  9.893e-02  -11.756  < 2e-16 ***
## StateTennessee                       4.093e-01  7.549e-02    5.422 6.93e-08 ***
## StateTexas                           1.301e-01  8.400e-02    1.549 0.121719    
## StateUtah                           -1.457e-01  1.057e-01   -1.378 0.168497    
## StateVermont                        -2.666e-01  1.036e-01   -2.573 0.010170 *  
## StateVirginia                       -2.278e-02  7.782e-02   -0.293 0.769767    
## StateWashington                      4.397e-01  1.030e-01    4.269 2.09e-05 ***
## StateWest Virginia                   5.873e-01  8.123e-02    7.230 7.87e-13 ***
## StateWisconsin                       5.717e-03  9.831e-02    0.058 0.953633    
## StateWyoming                         6.580e-02  1.013e-01    0.650 0.516008    
## Naloxone_Pharmacy_Yes_Redefined     -9.811e-02  7.429e-02   -1.321 0.186845    
## lag_num_pd_w_naloxone_yes           -8.489e-02  2.350e-02   -3.613 0.000313 ***
## Naloxone_Pharmacy_No_Redefined       1.138e-01  5.319e-02    2.139 0.032628 *  
## lag_num_pd_w_naloxone_no            -4.596e-02  5.561e-03   -8.265 3.15e-16 ***
## Medical_Marijuana_Redefined          2.423e-01  3.977e-02    6.094 1.42e-09 ***
## lag_num_pd_w_med_marijuana          -1.729e-02  3.095e-03   -5.588 2.76e-08 ***
## Recreational_Marijuana_Redefined    -2.513e-01  1.932e-01   -1.301 0.193593    
## lag_num_pd_w_rec_marijuana           7.860e-03  9.210e-02    0.085 0.931997    
## GSL_Redefined                        1.536e-01  5.511e-02    2.786 0.005401 ** 
## lag_num_pd_w_gsl                     2.092e-02  1.031e-02    2.030 0.042567 *  
## PDMP_Redefined                      -1.237e-01  2.988e-02   -4.139 3.69e-05 ***
## lag_num_pd_w_pdmp                    1.302e-02  2.805e-03    4.639 3.82e-06 ***
## Medicaid_Expansion_Redefined         9.799e-02  5.120e-02    1.914 0.055836 .  
## lag_num_pd_w_medicaid                9.359e-03  1.492e-02    0.627 0.530505    
## Intervention_Redefined              -3.543e-02  2.774e-02   -1.277 0.201742    
## lag_num_pd_w_tx                     -1.726e-02  2.463e-03   -7.009 3.70e-12 ***
## Time_Period_ID:RegionMidwest         9.007e-02  7.486e-03   12.031  < 2e-16 ***
## Time_Period_ID:RegionNortheast       9.834e-02  8.920e-03   11.025  < 2e-16 ***
## Time_Period_ID:RegionSouth           9.237e-02  6.535e-03   14.134  < 2e-16 ***
## Time_Period_ID:RegionWest            7.686e-02  7.700e-03    9.982  < 2e-16 ***
## RegionMidwest:I(Time_Period_ID^2)   -1.142e-03  2.436e-04   -4.689 3.01e-06 ***
## RegionNortheast:I(Time_Period_ID^2) -1.368e-03  2.894e-04   -4.727 2.51e-06 ***
## RegionSouth:I(Time_Period_ID^2)     -1.581e-03  2.101e-04   -7.523 9.42e-14 ***
## RegionWest:I(Time_Period_ID^2)      -1.102e-03  2.441e-04   -4.516 6.81e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2885 on 1426 degrees of freedom
## Multiple R-squared:  0.8348, Adjusted R-squared:  0.8264 
## F-statistic: 98.73 on 73 and 1426 DF,  p-value: < 2.2e-16

7.3.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset <- model.matrix(sensitivity_anlys_lin_post_tx_model_linear_time_subset)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx_subset <- coef(sensitivity_anlys_lin_post_tx_model_linear_time_subset)
#type = "response" to get the estimated probabilities
main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset <-
  compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset,
                    log(data_subset$prop_dead),
                    coefficient_values_log_fixed_lin_time_lin_post_tx_subset,
                    p = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset) - 50)
# format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset,4)

colnames(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset) <- c("conf.low", "estimate", "conf.high", "sd")
main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term <-
  rownames(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset)


main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$ci_95 <- 
  paste("95% CI = (", format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Linear/Quad Time Effects, 
       Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

7.3.2 Attributable Deaths

date_data_subset <- data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset <- date_data_subset[!duplicated(date_data_subset),]
attr_deaths_est_log_lin_time_lin_post_tx_subset <- attr_death_compute(data_subset,
                                                   main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset, 
                                                   lin_model = TRUE, tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))
attr_deaths_est_log_lin_time_lin_post_tx_subset <- merge(attr_deaths_est_log_lin_time_lin_post_tx_subset, date_data_subset, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_lin_post_tx_subset, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear and Quad. Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.3.3 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_lin_eff_log_outcome_lin_post_tx_subset <- boostrap_state_time(data_subset,
                                                     sensitivity_anlys_lin_post_tx_model_linear_time_subset,
                                                     lin_model = TRUE, 
                                                     tx_name = c("Intervention_Redefined", "lag_num_pd_w_tx"))

reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx_subset  <- Reduce("cbind", bootstrap_lin_eff_log_outcome_lin_post_tx_subset[[1]])

coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset  <- cbind(t(apply(reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx_subset, 1,
                                                                   quantile, 
                                                                 probs = c(.025, .975), na.rm = TRUE)),
                                             apply(reduced_coef_outcome_lin_eff_log_outcome_lin_post_tx_subset , 1, mean, na.rm = TRUE))
colnames(coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset ) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset  <- data.frame(coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset )
coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset$term <- rownames(coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset )

dwplot(coef_w_ci_lin_eff_log_outcome_lin_post_tx_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient and 95% CI of Linear Post Tx Effects Subset, 
       Using Bootstrap") + 
  scale_color_grey() 

list_attr_deaths_lin_eff_log_outcome_lin_post_tx_subset  <- lapply(bootstrap_lin_eff_log_outcome_lin_post_tx_subset[[2]],
                                                                   function(x){x$attr_deaths})
reduced_attr_deaths_lin_eff_log_outcome_lin_post_tx_subset <- Reduce("cbind", list_attr_deaths_lin_eff_log_outcome_lin_post_tx_subset)
attr_deaths_lin_eff_log_outcome_lin_post_tx_subset  <- 
  cbind(t(apply(reduced_attr_deaths_lin_eff_log_outcome_lin_post_tx_subset,  
                1,
                quantile, 
                probs = c(.025, .975), na.rm = TRUE)), 
        apply(reduced_attr_deaths_lin_eff_log_outcome_lin_post_tx_subset, 1, mean, na.rm = TRUE))
colnames(attr_deaths_lin_eff_log_outcome_lin_post_tx_subset) <- c("conf.low", "conf.high", "estimate")
attr_deaths_lin_eff_log_outcome_lin_post_tx_subset <-
  data.frame(attr_deaths_lin_eff_log_outcome_lin_post_tx_subset)
attr_deaths_lin_eff_log_outcome_lin_post_tx_subset$Time_Period_Start <- unique(data_subset$Time_Period_Start)

ggplot(attr_deaths_lin_eff_log_outcome_lin_post_tx_subset, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.4 Event Study

7.4.1 Model Fitting

#create a formula for the gam model which includes the state effects, smoothed time effects, policy measures, 
#the periods before the intervention (excluding 1 period and 34 periods before intervention)
#the intervention period, and the periods after the intervention

formula_event_study_log_fixed_lin_time <- formula(paste("log(prop_dead) ~ State +
                                           Time_Period_ID:Region  +
                                           I(Time_Period_ID^2):Region + 
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                     paste(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)-2), 
                                            function(x)paste("neg_", x, "_pd", sep = "")), collapse = "+"),
                                     "+",
                                     paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                              function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_event_study_model_log_fixed_lin_time<-lm(formula_event_study_log_fixed_lin_time,
                                         data = sensitivity_anlys_event_study_data)

summary(sensitivity_anlys_event_study_model_log_fixed_lin_time)
## 
## Call:
## lm(formula = formula_event_study_log_fixed_lin_time, data = sensitivity_anlys_event_study_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -1.94531 -0.13453  0.01384  0.15668  1.02461 
## 
## Coefficients:
##                                       Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                         -1.075e+01  9.544e-02 -112.655  < 2e-16 ***
## StateAlaska                          3.681e-01  1.075e-01    3.426 0.000626 ***
## StateArizona                         5.417e-01  9.088e-02    5.961 2.99e-09 ***
## StateArkansas                       -5.239e-01  7.259e-02   -7.216 7.74e-13 ***
## StateCalifornia                      2.073e-01  1.047e-01    1.979 0.047963 *  
## StateColorado                        2.631e-01  9.303e-02    2.828 0.004731 ** 
## StateConnecticut                     9.640e-02  9.110e-02    1.058 0.290105    
## StateDelaware                        1.371e-01  9.579e-02    1.431 0.152515    
## StateFlorida                         4.217e-01  8.768e-02    4.810 1.63e-06 ***
## StateGeorgia                         1.256e-01  9.359e-02    1.342 0.179679    
## StateHawaii                         -1.940e-01  9.856e-02   -1.968 0.049189 *  
## StateIdaho                           1.268e-01  1.020e-01    1.244 0.213751    
## StateIllinois                        6.142e-02  9.772e-02    0.629 0.529744    
## StateIndiana                        -7.033e-02  8.764e-02   -0.802 0.422390    
## StateIowa                           -7.574e-01  9.173e-02   -8.257 2.80e-16 ***
## StateKansas                         -3.589e-01  8.637e-02   -4.155 3.40e-05 ***
## StateKentucky                        6.751e-01  7.308e-02    9.238  < 2e-16 ***
## StateLouisiana                       4.021e-01  7.420e-02    5.419 6.78e-08 ***
## StateMaine                          -6.443e-02  9.467e-02   -0.681 0.496190    
## StateMaryland                       -1.450e+00  8.167e-02  -17.755  < 2e-16 ***
## StateMassachusetts                  -2.100e-01  9.102e-02   -2.307 0.021172 *  
## StateMichigan                       -8.755e-02  8.965e-02   -0.977 0.328865    
## StateMinnesota                      -8.065e-01  8.839e-02   -9.124  < 2e-16 ***
## StateMississippi                    -9.381e-02  8.422e-02   -1.114 0.265434    
## StateMissouri                        4.407e-02  9.107e-02    0.484 0.628464    
## StateMontana                        -1.026e-01  9.498e-02   -1.080 0.280276    
## StateNebraska                       -1.044e+00  9.408e-02  -11.101  < 2e-16 ***
## StateNevada                          7.785e-01  9.129e-02    8.527  < 2e-16 ***
## StateNew Hampshire                  -2.938e-03  9.185e-02   -0.032 0.974486    
## StateNew Jersey                      1.872e-02  1.003e-01    0.187 0.851990    
## StateNew Mexico                      8.850e-01  8.919e-02    9.923  < 2e-16 ***
## StateNew York                       -2.568e-01  9.076e-02   -2.830 0.004707 ** 
## StateNorth Carolina                  2.672e-01  7.077e-02    3.776 0.000164 ***
## StateNorth Dakota                   -1.325e+00  9.366e-02  -14.146  < 2e-16 ***
## StateOhio                            4.498e-01  1.090e-01    4.128 3.83e-05 ***
## StateOklahoma                        4.440e-01  7.182e-02    6.181 7.79e-10 ***
## StateOregon                         -4.697e-02  8.988e-02   -0.523 0.601342    
## StatePennsylvania                    5.675e-01  1.067e-01    5.316 1.19e-07 ***
## StateRhode Island                   -4.515e-01  1.034e-01   -4.368 1.32e-05 ***
## StateSouth Carolina                  1.590e-01  9.194e-02    1.730 0.083853 .  
## StateSouth Dakota                   -1.233e+00  1.078e-01  -11.438  < 2e-16 ***
## StateTennessee                       4.662e-01  6.651e-02    7.009 3.34e-12 ***
## StateTexas                           9.686e-02  8.446e-02    1.147 0.251623    
## StateUtah                            2.479e-01  8.977e-02    2.761 0.005817 ** 
## StateVermont                        -3.360e-01  9.209e-02   -3.648 0.000271 ***
## StateVirginia                        3.640e-02  7.555e-02    0.482 0.629990    
## StateWashington                      2.882e-01  8.980e-02    3.210 0.001351 ** 
## StateWest Virginia                   7.654e-01  7.839e-02    9.763  < 2e-16 ***
## StateWisconsin                      -8.544e-02  9.307e-02   -0.918 0.358705    
## StateWyoming                         2.723e-01  8.661e-02    3.144 0.001695 ** 
## Naloxone_Pharmacy_Yes_Redefined      5.742e-02  3.942e-02    1.457 0.145393    
## Naloxone_Pharmacy_No_Redefined       1.750e-02  3.877e-02    0.452 0.651676    
## Medical_Marijuana_Redefined          2.179e-01  3.148e-02    6.922 6.11e-12 ***
## Recreational_Marijuana_Redefined    -7.658e-02  4.945e-02   -1.549 0.121609    
## GSL_Redefined                        5.952e-02  3.170e-02    1.877 0.060623 .  
## PDMP_Redefined                      -1.878e-01  2.513e-02   -7.472 1.20e-13 ***
## Medicaid_Expansion_Redefined         7.906e-02  3.021e-02    2.617 0.008940 ** 
## neg_2_pd                             2.811e-02  6.097e-02    0.461 0.644868    
## neg_3_pd                             3.165e-02  6.190e-02    0.511 0.609135    
## neg_4_pd                             7.850e-03  6.316e-02    0.124 0.901104    
## neg_5_pd                             3.932e-03  6.385e-02    0.062 0.950903    
## neg_6_pd                             7.330e-03  6.594e-02    0.111 0.911513    
## neg_7_pd                            -4.042e-02  6.705e-02   -0.603 0.546673    
## neg_8_pd                            -1.028e-01  6.875e-02   -1.496 0.134855    
## neg_9_pd                            -3.113e-02  7.163e-02   -0.435 0.663913    
## neg_10_pd                            1.448e-02  7.375e-02    0.196 0.844339    
## neg_11_pd                            1.979e-02  7.618e-02    0.260 0.795086    
## neg_12_pd                            1.120e-01  8.058e-02    1.390 0.164807    
## neg_13_pd                            1.633e-02  8.271e-02    0.197 0.843482    
## neg_14_pd                           -1.026e-02  8.494e-02   -0.121 0.903825    
## neg_15_pd                           -5.217e-02  8.733e-02   -0.597 0.550303    
## neg_16_pd                           -3.652e-02  9.162e-02   -0.399 0.690262    
## neg_17_pd                           -4.312e-02  9.690e-02   -0.445 0.656356    
## neg_18_pd                           -3.964e-02  1.003e-01   -0.395 0.692646    
## neg_19_pd                           -1.457e-01  1.036e-01   -1.407 0.159592    
## neg_20_pd                           -1.913e-01  1.096e-01   -1.745 0.081105 .  
## neg_21_pd                           -1.223e-01  1.168e-01   -1.048 0.294990    
## neg_22_pd                           -1.057e-01  1.195e-01   -0.885 0.376474    
## neg_23_pd                           -1.248e-01  1.238e-01   -1.008 0.313537    
## neg_24_pd                           -1.803e-01  1.330e-01   -1.355 0.175496    
## neg_25_pd                           -4.070e-02  1.356e-01   -0.300 0.764130    
## neg_26_pd                            3.718e-02  1.414e-01    0.263 0.792632    
## neg_27_pd                           -2.152e-01  1.570e-01   -1.371 0.170645    
## neg_28_pd                           -2.102e-01  1.595e-01   -1.318 0.187786    
## neg_29_pd                            5.712e-03  1.686e-01    0.034 0.972974    
## neg_30_pd                           -6.349e-02  1.793e-01   -0.354 0.723254    
## neg_31_pd                           -7.353e-02  1.818e-01   -0.404 0.685918    
## neg_32_pd                           -5.692e-03  1.968e-01   -0.029 0.976933    
## neg_33_pd                            6.658e-02  2.498e-01    0.267 0.789851    
## pos_0_pd                            -2.301e-02  6.072e-02   -0.379 0.704770    
## pos_1_pd                            -5.961e-02  6.127e-02   -0.973 0.330724    
## pos_2_pd                            -2.841e-02  6.204e-02   -0.458 0.647008    
## pos_3_pd                            -6.438e-02  6.304e-02   -1.021 0.307267    
## pos_4_pd                            -6.953e-02  6.423e-02   -1.082 0.279202    
## pos_5_pd                            -1.105e-01  6.567e-02   -1.682 0.092724 .  
## pos_6_pd                            -1.105e-01  6.749e-02   -1.637 0.101807    
## pos_7_pd                            -1.066e-01  6.950e-02   -1.534 0.125178    
## pos_8_pd                            -1.617e-01  7.211e-02   -2.243 0.025007 *  
## pos_9_pd                            -1.812e-01  7.458e-02   -2.429 0.015225 *  
## pos_10_pd                           -1.955e-01  7.677e-02   -2.546 0.010965 *  
## pos_11_pd                           -1.992e-01  7.945e-02   -2.508 0.012236 *  
## pos_12_pd                           -2.016e-01  8.223e-02   -2.451 0.014333 *  
## pos_13_pd                           -2.705e-01  8.478e-02   -3.191 0.001444 ** 
## pos_14_pd                           -2.628e-01  8.835e-02   -2.975 0.002969 ** 
## pos_15_pd                           -2.708e-01  9.162e-02   -2.956 0.003159 ** 
## pos_16_pd                           -2.885e-01  9.454e-02   -3.051 0.002311 ** 
## pos_17_pd                           -2.850e-01  9.847e-02   -2.894 0.003851 ** 
## pos_18_pd                           -2.747e-01  1.020e-01   -2.694 0.007123 ** 
## pos_19_pd                           -2.633e-01  1.049e-01   -2.511 0.012133 *  
## pos_20_pd                           -2.747e-01  1.092e-01   -2.517 0.011935 *  
## pos_21_pd                           -3.075e-01  1.135e-01   -2.708 0.006830 ** 
## pos_22_pd                           -3.064e-01  1.171e-01   -2.617 0.008954 ** 
## pos_23_pd                           -3.240e-01  1.211e-01   -2.675 0.007548 ** 
## pos_24_pd                           -3.686e-01  1.266e-01   -2.911 0.003643 ** 
## pos_25_pd                           -3.300e-01  1.319e-01   -2.502 0.012447 *  
## pos_26_pd                           -3.265e-01  1.352e-01   -2.415 0.015810 *  
## pos_27_pd                           -3.660e-01  1.389e-01   -2.636 0.008465 ** 
## pos_28_pd                           -3.402e-01  1.420e-01   -2.395 0.016699 *  
## pos_29_pd                           -3.473e-01  1.495e-01   -2.323 0.020313 *  
## pos_30_pd                           -2.843e-01  1.542e-01   -1.844 0.065313 .  
## pos_31_pd                           -2.447e-01  1.595e-01   -1.534 0.125160    
## pos_32_pd                           -3.009e-01  1.696e-01   -1.774 0.076169 .  
## pos_33_pd                           -3.286e-01  1.760e-01   -1.867 0.062044 .  
## pos_34_pd                           -3.185e-01  1.793e-01   -1.777 0.075803 .  
## pos_35_pd                           -5.112e-01  1.958e-01   -2.611 0.009092 ** 
## pos_36_pd                           -5.411e-01  1.988e-01   -2.721 0.006568 ** 
## pos_37_pd                           -5.613e-01  2.208e-01   -2.542 0.011107 *  
## pos_38_pd                           -5.073e-01  2.698e-01   -1.880 0.060223 .  
## pos_39_pd                           -5.236e-01  2.725e-01   -1.921 0.054831 .  
## Time_Period_ID:RegionMidwest         8.336e-02  6.350e-03   13.128  < 2e-16 ***
## Time_Period_ID:RegionNortheast       6.990e-02  7.238e-03    9.657  < 2e-16 ***
## Time_Period_ID:RegionSouth           7.534e-02  6.113e-03   12.326  < 2e-16 ***
## Time_Period_ID:RegionWest            5.984e-02  6.325e-03    9.460  < 2e-16 ***
## RegionMidwest:I(Time_Period_ID^2)   -7.722e-04  1.317e-04   -5.865 5.29e-09 ***
## RegionNortheast:I(Time_Period_ID^2) -3.276e-04  1.529e-04   -2.142 0.032326 *  
## RegionSouth:I(Time_Period_ID^2)     -7.387e-04  1.228e-04   -6.017 2.13e-09 ***
## RegionWest:I(Time_Period_ID^2)      -6.900e-04  1.325e-04   -5.206 2.15e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.297 on 1863 degrees of freedom
## Multiple R-squared:  0.8471, Adjusted R-squared:  0.8359 
## F-statistic:  75.9 on 136 and 1863 DF,  p-value: < 2.2e-16

7.4.2 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_event_study_log_fixed_lin_time <-
  model.matrix(sensitivity_anlys_event_study_model_log_fixed_lin_time)

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_event_study_log_fixed_lin_time <- coef(sensitivity_anlys_event_study_model_log_fixed_lin_time)
#type = "response" to get the estimated probabilities
sensitivity_anlys_event_study_sd_and_ci_log_fixed_lin_time <- 
  compute_sd_and_CI((full_df_w_basis_functions_sensitivity_anlys_event_study_log_fixed_lin_time), 
                                                             log(sensitivity_anlys_event_study_data$prop_dead),
                                                             coefficient_values_sensitivity_anlys_event_study_log_fixed_lin_time,
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_event_study_log_fixed_lin_time) - 50)
# (sensitivity_anlys_event_study_sd_and_ci_log_fixed_lin_time)
# write.csv(format(round(sensitivity_anlys_event_study_sd_and_ci, 3), nsmall = 3), "./Data/event_study_coef_and_ci.csv")

7.4.3 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_event_study_log_fixed_lin_time <- sensitivity_anlys_event_study_sd_and_ci_log_fixed_lin_time %>%
  mutate(term = rownames(sensitivity_anlys_event_study_sd_and_ci_log_fixed_lin_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}), 
                     sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")})))
colnames(plot_event_study_log_fixed_lin_time) <- c("term", "estimate", "conf.low", "conf.high")

dwplot(plot_event_study_log_fixed_lin_time, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "States Excluded", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Pre-Intervention and Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

7.4.4 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_lin_eff_log_outcome_event_study<- boostrap_state_time(sensitivity_anlys_event_study_data,
                                                     sensitivity_anlys_event_study_model_log_fixed_lin_time,
                                                     lin_model = FALSE)

reduced_coef_outcome_lin_eff_log_outcome_event_study  <- Reduce("cbind", bootstrap_lin_eff_log_outcome_event_study [[1]])

coef_w_ci_lin_eff_log_outcome_event_study  <- cbind(t(apply(reduced_coef_outcome_lin_eff_log_outcome_event_study , 1, quantile, 
                                                                 probs = c(.025, .975), na.rm = TRUE)),
                                             apply(reduced_coef_outcome_lin_eff_log_outcome_event_study , 1, mean, na.rm = TRUE))
colnames(coef_w_ci_lin_eff_log_outcome_event_study ) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_lin_eff_log_outcome_event_study  <- data.frame(coef_w_ci_lin_eff_log_outcome_event_study )
coef_w_ci_lin_eff_log_outcome_event_study $term <- rownames(coef_w_ci_lin_eff_log_outcome_event_study )

dwplot(coef_w_ci_lin_eff_log_outcome_event_study, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}),
                       sapply(2:(max(merged_main_time_data_int$intervention_time_id, na.rm = TRUE) - 2), 
                                   function(x){paste("neg_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Pre-Intervention and Post-Intervention Periods, Using Bootstrap") + 
  scale_color_grey() + 
  coord_flip()  +
  geom_hline(yintercept = 33, col = "red", linetype = "dashed")

7.5 Analysis With Only Periods After Treatment

formula_post_tx_log_fixed_lin_time <- formula(paste("log(prop_dead)~ State +
                                           Time_Period_ID:Region  +
                                           I(Time_Period_ID^2):Region + 
                                           Naloxone_Pharmacy_Yes_Redefined +
                                           Naloxone_Pharmacy_No_Redefined +
                                           Medical_Marijuana_Redefined +
                                           Recreational_Marijuana_Redefined +
                                           GSL_Redefined +
                                           PDMP_Redefined +
                                           Medicaid_Expansion_Redefined +",
                                     paste(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)),
                              function(x)paste("pos_", x, "_pd", sep = "")), collapse = "+")))
#run the gam model
sensitivity_anlys_post_tx_model_log_fixed_lin_time<-lm(formula_post_tx_log_fixed_lin_time,
                                         data = sensitivity_anlys_event_study_data)
summary(sensitivity_anlys_post_tx_model_log_fixed_lin_time)
## 
## Call:
## lm(formula = formula_post_tx_log_fixed_lin_time, data = sensitivity_anlys_event_study_data)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -2.02721 -0.13368  0.01535  0.15896  1.01698 
## 
## Coefficients:
##                                       Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                         -1.080e+01  5.921e-02 -182.420  < 2e-16 ***
## StateAlaska                          2.967e-01  8.967e-02    3.309 0.000954 ***
## StateArizona                         4.987e-01  8.586e-02    5.808 7.40e-09 ***
## StateArkansas                       -5.591e-01  6.798e-02   -8.225 3.59e-16 ***
## StateCalifornia                      2.543e-01  9.219e-02    2.759 0.005853 ** 
## StateColorado                        2.249e-01  8.912e-02    2.524 0.011690 *  
## StateConnecticut                     1.058e-01  9.005e-02    1.175 0.240113    
## StateDelaware                        6.701e-02  7.023e-02    0.954 0.340179    
## StateFlorida                         4.733e-01  7.108e-02    6.658 3.62e-11 ***
## StateGeorgia                         1.862e-01  7.271e-02    2.561 0.010528 *  
## StateHawaii                         -2.335e-01  8.970e-02   -2.604 0.009294 ** 
## StateIdaho                           5.990e-02  8.696e-02    0.689 0.491038    
## StateIllinois                        1.075e-01  8.783e-02    1.225 0.220913    
## StateIndiana                        -8.394e-02  8.651e-02   -0.970 0.331978    
## StateIowa                           -7.274e-01  8.635e-02   -8.424  < 2e-16 ***
## StateKansas                         -3.463e-01  8.567e-02   -4.042 5.50e-05 ***
## StateKentucky                        6.379e-01  6.792e-02    9.392  < 2e-16 ***
## StateLouisiana                       4.279e-01  6.774e-02    6.317 3.32e-10 ***
## StateMaine                          -6.923e-02  9.276e-02   -0.746 0.455563    
## StateMaryland                       -1.409e+00  7.030e-02  -20.042  < 2e-16 ***
## StateMassachusetts                  -2.044e-01  8.995e-02   -2.273 0.023163 *  
## StateMichigan                       -6.756e-02  8.707e-02   -0.776 0.437842    
## StateMinnesota                      -7.958e-01  8.774e-02   -9.070  < 2e-16 ***
## StateMississippi                    -1.571e-01  6.783e-02   -2.316 0.020656 *  
## StateMissouri                        6.991e-02  8.739e-02    0.800 0.423777    
## StateMontana                        -7.476e-02  8.823e-02   -0.847 0.396923    
## StateNebraska                       -1.082e+00  8.754e-02  -12.362  < 2e-16 ***
## StateNevada                          7.920e-01  8.866e-02    8.933  < 2e-16 ***
## StateNew Hampshire                  -1.481e-02  8.975e-02   -0.165 0.868915    
## StateNew Jersey                      6.674e-02  9.089e-02    0.734 0.462820    
## StateNew Mexico                      8.669e-01  8.813e-02    9.836  < 2e-16 ***
## StateNew York                       -2.457e-01  8.967e-02   -2.740 0.006203 ** 
## StateNorth Carolina                  2.847e-01  6.712e-02    4.242 2.32e-05 ***
## StateNorth Dakota                   -1.364e+00  8.688e-02  -15.699  < 2e-16 ***
## StateOhio                            5.175e-01  8.962e-02    5.774 9.00e-09 ***
## StateOklahoma                        4.109e-01  6.754e-02    6.083 1.42e-09 ***
## StateOregon                         -6.071e-02  8.896e-02   -0.682 0.495062    
## StatePennsylvania                    6.255e-01  9.318e-02    6.713 2.52e-11 ***
## StateRhode Island                   -5.083e-01  9.098e-02   -5.587 2.64e-08 ***
## StateSouth Carolina                  8.840e-02  6.844e-02    1.292 0.196611    
## StateSouth Dakota                   -1.296e+00  8.821e-02  -14.690  < 2e-16 ***
## StateTennessee                       4.656e-01  6.645e-02    7.007 3.38e-12 ***
## StateTexas                           1.433e-01  7.069e-02    2.027 0.042752 *  
## StateUtah                            2.650e-01  8.636e-02    3.069 0.002177 ** 
## StateVermont                        -3.443e-01  9.008e-02   -3.822 0.000136 ***
## StateVirginia                        6.727e-02  6.809e-02    0.988 0.323272    
## StateWashington                      2.842e-01  8.938e-02    3.179 0.001500 ** 
## StateWest Virginia                   7.171e-01  6.823e-02   10.510  < 2e-16 ***
## StateWisconsin                      -5.121e-02  8.648e-02   -0.592 0.553788    
## StateWyoming                         2.618e-01  8.589e-02    3.048 0.002333 ** 
## Naloxone_Pharmacy_Yes_Redefined      5.810e-02  3.932e-02    1.478 0.139643    
## Naloxone_Pharmacy_No_Redefined       1.576e-02  3.864e-02    0.408 0.683337    
## Medical_Marijuana_Redefined          2.221e-01  3.123e-02    7.114 1.59e-12 ***
## Recreational_Marijuana_Redefined    -7.031e-02  4.881e-02   -1.440 0.149917    
## GSL_Redefined                        5.969e-02  3.161e-02    1.888 0.059123 .  
## PDMP_Redefined                      -1.832e-01  2.473e-02   -7.407 1.94e-13 ***
## Medicaid_Expansion_Redefined         7.554e-02  3.002e-02    2.517 0.011931 *  
## pos_0_pd                            -4.391e-02  4.635e-02   -0.948 0.343494    
## pos_1_pd                            -8.452e-02  4.681e-02   -1.805 0.071162 .  
## pos_2_pd                            -5.723e-02  4.729e-02   -1.210 0.226343    
## pos_3_pd                            -9.708e-02  4.785e-02   -2.029 0.042608 *  
## pos_4_pd                            -1.061e-01  4.845e-02   -2.189 0.028738 *  
## pos_5_pd                            -1.506e-01  4.925e-02   -3.057 0.002265 ** 
## pos_6_pd                            -1.542e-01  5.022e-02   -3.071 0.002165 ** 
## pos_7_pd                            -1.543e-01  5.111e-02   -3.019 0.002566 ** 
## pos_8_pd                            -2.137e-01  5.253e-02   -4.068 4.94e-05 ***
## pos_9_pd                            -2.373e-01  5.371e-02   -4.417 1.06e-05 ***
## pos_10_pd                           -2.554e-01  5.451e-02   -4.685 3.00e-06 ***
## pos_11_pd                           -2.633e-01  5.570e-02   -4.727 2.45e-06 ***
## pos_12_pd                           -2.699e-01  5.690e-02   -4.742 2.27e-06 ***
## pos_13_pd                           -3.427e-01  5.776e-02   -5.932 3.54e-09 ***
## pos_14_pd                           -3.391e-01  5.988e-02   -5.663 1.71e-08 ***
## pos_15_pd                           -3.515e-01  6.129e-02   -5.735 1.13e-08 ***
## pos_16_pd                           -3.731e-01  6.233e-02   -5.987 2.55e-09 ***
## pos_17_pd                           -3.741e-01  6.473e-02   -5.779 8.79e-09 ***
## pos_18_pd                           -3.678e-01  6.643e-02   -5.537 3.51e-08 ***
## pos_19_pd                           -3.599e-01  6.762e-02   -5.322 1.15e-07 ***
## pos_20_pd                           -3.754e-01  7.048e-02   -5.326 1.12e-07 ***
## pos_21_pd                           -4.122e-01  7.343e-02   -5.614 2.27e-08 ***
## pos_22_pd                           -4.150e-01  7.523e-02   -5.517 3.92e-08 ***
## pos_23_pd                           -4.370e-01  7.719e-02   -5.661 1.73e-08 ***
## pos_24_pd                           -4.847e-01  8.208e-02   -5.905 4.17e-09 ***
## pos_25_pd                           -4.491e-01  8.668e-02   -5.181 2.44e-07 ***
## pos_26_pd                           -4.496e-01  8.775e-02   -5.124 3.30e-07 ***
## pos_27_pd                           -4.935e-01  8.904e-02   -5.542 3.40e-08 ***
## pos_28_pd                           -4.711e-01  9.051e-02   -5.205 2.15e-07 ***
## pos_29_pd                           -4.818e-01  9.857e-02   -4.887 1.11e-06 ***
## pos_30_pd                           -4.227e-01  1.018e-01   -4.153 3.43e-05 ***
## pos_31_pd                           -3.879e-01  1.054e-01   -3.682 0.000238 ***
## pos_32_pd                           -4.492e-01  1.161e-01   -3.870 0.000113 ***
## pos_33_pd                           -4.813e-01  1.214e-01   -3.964 7.64e-05 ***
## pos_34_pd                           -4.751e-01  1.227e-01   -3.872 0.000111 ***
## pos_35_pd                           -6.716e-01  1.426e-01   -4.709 2.66e-06 ***
## pos_36_pd                           -7.051e-01  1.438e-01   -4.903 1.02e-06 ***
## pos_37_pd                           -7.306e-01  1.695e-01   -4.310 1.71e-05 ***
## pos_38_pd                           -6.815e-01  2.272e-01   -2.999 0.002742 ** 
## pos_39_pd                           -7.017e-01  2.282e-01   -3.074 0.002139 ** 
## Time_Period_ID:RegionMidwest         8.691e-02  5.282e-03   16.453  < 2e-16 ***
## Time_Period_ID:RegionNortheast       7.370e-02  6.058e-03   12.165  < 2e-16 ***
## Time_Period_ID:RegionSouth           7.974e-02  4.685e-03   17.022  < 2e-16 ***
## Time_Period_ID:RegionWest            6.430e-02  5.133e-03   12.528  < 2e-16 ***
## RegionMidwest:I(Time_Period_ID^2)   -7.645e-04  1.308e-04   -5.846 5.92e-09 ***
## RegionNortheast:I(Time_Period_ID^2) -3.293e-04  1.518e-04   -2.170 0.030138 *  
## RegionSouth:I(Time_Period_ID^2)     -7.483e-04  1.209e-04   -6.191 7.29e-10 ***
## RegionWest:I(Time_Period_ID^2)      -7.087e-04  1.313e-04   -5.397 7.61e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2967 on 1895 degrees of freedom
## Multiple R-squared:  0.8447, Adjusted R-squared:  0.8362 
## F-statistic: 99.13 on 104 and 1895 DF,  p-value: < 2.2e-16

7.5.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_sensitivity_anlys_post_tx_log_fixed_lin_time <- model.matrix(sensitivity_anlys_post_tx_model_log_fixed_lin_time)

#estimate the 95% CI and SD
coefficient_values_sensitivity_anlys_post_tx_log_fixed_lin_time <- coef(sensitivity_anlys_post_tx_model_log_fixed_lin_time)

sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time <-
  compute_sd_and_CI((full_df_w_basis_functions_sensitivity_anlys_post_tx_log_fixed_lin_time), 
                                                         log(sensitivity_anlys_event_study_data$prop_dead),
                                                         coefficient_values_sensitivity_anlys_post_tx_log_fixed_lin_time,
                    p = ncol(full_df_w_basis_functions_sensitivity_anlys_post_tx_log_fixed_lin_time) - 50)
sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time
##                                           lb_coef   coef_values       ub_coef
## (Intercept)                         -1.089784e+01 -1.080161e+01 -1.070539e+01
## StateAlaska                          1.377462e-01  2.967280e-01  4.557098e-01
## StateArizona                         3.572478e-01  4.986602e-01  6.400727e-01
## StateArkansas                       -6.645857e-01 -5.590763e-01 -4.535669e-01
## StateCalifornia                      8.192892e-02  2.543418e-01  4.267547e-01
## StateColorado                        8.411816e-02  2.249102e-01  3.657023e-01
## StateConnecticut                    -1.274304e-01  1.058193e-01  3.390691e-01
## StateDelaware                       -6.062897e-02  6.700585e-02  1.946407e-01
## StateFlorida                         3.655075e-01  4.732739e-01  5.810402e-01
## StateGeorgia                         8.581420e-02  1.861762e-01  2.865383e-01
## StateHawaii                         -3.790756e-01 -2.335472e-01 -8.801869e-02
## StateIdaho                          -8.147777e-02  5.990027e-02  2.012783e-01
## StateIllinois                       -6.146294e-02  1.075467e-01  2.765563e-01
## StateIndiana                        -2.298681e-01 -8.394337e-02  6.198141e-02
## StateIowa                           -8.706980e-01 -7.274116e-01 -5.841252e-01
## StateKansas                         -4.783749e-01 -3.463026e-01 -2.142302e-01
## StateKentucky                        5.483942e-01  6.378878e-01  7.273813e-01
## StateLouisiana                       3.277501e-01  4.278619e-01  5.279737e-01
## StateMaine                          -3.088537e-01 -6.923281e-02  1.703881e-01
## StateMaryland                       -1.611573e+00 -1.409027e+00 -1.206480e+00
## StateMassachusetts                  -5.388172e-01 -2.044092e-01  1.299989e-01
## StateMichigan                       -1.985843e-01 -6.756352e-02  6.345722e-02
## StateMinnesota                      -9.350398e-01 -7.958091e-01 -6.565784e-01
## StateMississippi                    -2.679502e-01 -1.571042e-01 -4.625827e-02
## StateMissouri                       -5.911046e-02  6.991381e-02  1.989381e-01
## StateMontana                        -2.247769e-01 -7.476100e-02  7.525488e-02
## StateNebraska                       -1.216469e+00 -1.082111e+00 -9.477529e-01
## StateNevada                          6.468741e-01  7.919969e-01  9.371197e-01
## StateNew Hampshire                  -2.404466e-01 -1.481407e-02  2.108185e-01
## StateNew Jersey                     -1.665574e-01  6.674317e-02  3.000437e-01
## StateNew Mexico                      7.068903e-01  8.668822e-01  1.026874e+00
## StateNew York                       -4.685869e-01 -2.456932e-01 -2.279944e-02
## StateNorth Carolina                  1.978452e-01  2.847260e-01  3.716067e-01
## StateNorth Dakota                   -1.546468e+00 -1.363971e+00 -1.181474e+00
## StateOhio                            3.605855e-01  5.174907e-01  6.743959e-01
## StateOklahoma                        3.010775e-01  4.108628e-01  5.206481e-01
## StateOregon                         -2.016273e-01 -6.070866e-02  8.020998e-02
## StatePennsylvania                    4.223253e-01  6.254850e-01  8.286446e-01
## StateRhode Island                   -9.035257e-01 -5.083039e-01 -1.130821e-01
## StateSouth Carolina                 -1.049984e-02  8.840328e-02  1.873064e-01
## StateSouth Dakota                   -1.456686e+00 -1.295833e+00 -1.134980e+00
## StateTennessee                       3.811316e-01  4.656090e-01  5.500865e-01
## StateTexas                           2.771875e-02  1.433278e-01  2.589369e-01
## StateUtah                            3.872767e-02  2.650449e-01  4.913622e-01
## StateVermont                        -5.901314e-01 -3.443276e-01 -9.852372e-02
## StateVirginia                       -2.766106e-02  6.727204e-02  1.622051e-01
## StateWashington                      1.419634e-01  2.841846e-01  4.264057e-01
## StateWest Virginia                   5.765619e-01  7.170779e-01  8.575938e-01
## StateWisconsin                      -1.786456e-01 -5.121088e-02  7.622383e-02
## StateWyoming                         9.222745e-02  2.618196e-01  4.314117e-01
## Naloxone_Pharmacy_Yes_Redefined     -3.675226e-03  5.810410e-02  1.198834e-01
## Naloxone_Pharmacy_No_Redefined      -4.544605e-02  1.576258e-02  7.697122e-02
## Medical_Marijuana_Redefined          1.514954e-01  2.221351e-01  2.927748e-01
## Recreational_Marijuana_Redefined    -1.458147e-01 -7.030772e-02  5.199212e-03
## GSL_Redefined                        7.240317e-03  5.969397e-02  1.121476e-01
## PDMP_Redefined                      -2.356820e-01 -1.831820e-01 -1.306820e-01
## Medicaid_Expansion_Redefined         2.548103e-02  7.554247e-02  1.256039e-01
## pos_0_pd                            -1.232220e-01 -4.391484e-02  3.539236e-02
## pos_1_pd                            -1.753316e-01 -8.451575e-02  6.300065e-03
## pos_2_pd                            -1.345725e-01 -5.723292e-02  2.010664e-02
## pos_3_pd                            -1.767463e-01 -9.707618e-02 -1.740610e-02
## pos_4_pd                            -1.864027e-01 -1.060528e-01 -2.570288e-02
## pos_5_pd                            -2.338331e-01 -1.505587e-01 -6.728426e-02
## pos_6_pd                            -2.431210e-01 -1.542152e-01 -6.530942e-02
## pos_7_pd                            -2.484738e-01 -1.543273e-01 -6.018072e-02
## pos_8_pd                            -3.019100e-01 -2.136631e-01 -1.254163e-01
## pos_9_pd                            -3.339051e-01 -2.372509e-01 -1.405966e-01
## pos_10_pd                           -3.551988e-01 -2.553866e-01 -1.555745e-01
## pos_11_pd                           -3.537798e-01 -2.633042e-01 -1.728287e-01
## pos_12_pd                           -3.681921e-01 -2.698599e-01 -1.715278e-01
## pos_13_pd                           -4.602296e-01 -3.426755e-01 -2.251215e-01
## pos_14_pd                           -4.594010e-01 -3.390926e-01 -2.187843e-01
## pos_15_pd                           -4.618573e-01 -3.515039e-01 -2.411505e-01
## pos_16_pd                           -4.903779e-01 -3.731365e-01 -2.558951e-01
## pos_17_pd                           -4.941843e-01 -3.740702e-01 -2.539562e-01
## pos_18_pd                           -4.878219e-01 -3.678096e-01 -2.477973e-01
## pos_19_pd                           -4.749640e-01 -3.599141e-01 -2.448642e-01
## pos_20_pd                           -4.986432e-01 -3.754216e-01 -2.522001e-01
## pos_21_pd                           -5.463634e-01 -4.122110e-01 -2.780585e-01
## pos_22_pd                           -5.478495e-01 -4.150284e-01 -2.822073e-01
## pos_23_pd                           -5.794203e-01 -4.369711e-01 -2.945219e-01
## pos_24_pd                           -6.367526e-01 -4.846949e-01 -3.326372e-01
## pos_25_pd                           -6.223804e-01 -4.491097e-01 -2.758390e-01
## pos_26_pd                           -6.176132e-01 -4.496148e-01 -2.816163e-01
## pos_27_pd                           -6.721251e-01 -4.934802e-01 -3.148352e-01
## pos_28_pd                           -6.582506e-01 -4.710687e-01 -2.838869e-01
## pos_29_pd                           -6.826784e-01 -4.817697e-01 -2.808610e-01
## pos_30_pd                           -6.304967e-01 -4.227249e-01 -2.149530e-01
## pos_31_pd                           -6.222924e-01 -3.878906e-01 -1.534888e-01
## pos_32_pd                           -6.725618e-01 -4.492206e-01 -2.258793e-01
## pos_33_pd                           -7.607229e-01 -4.813352e-01 -2.019476e-01
## pos_34_pd                           -7.603424e-01 -4.751498e-01 -1.899573e-01
## pos_35_pd                           -9.116375e-01 -6.715534e-01 -4.314694e-01
## pos_36_pd                           -9.468931e-01 -7.050687e-01 -4.632442e-01
## pos_37_pd                           -9.597621e-01 -7.305666e-01 -5.013711e-01
## pos_38_pd                           -8.739929e-01 -6.815169e-01 -4.890408e-01
## pos_39_pd                           -9.575988e-01 -7.016723e-01 -4.457458e-01
## Time_Period_ID:RegionMidwest         7.736058e-02  8.691258e-02  9.646458e-02
## Time_Period_ID:RegionNortheast       5.286972e-02  7.369820e-02  9.452667e-02
## Time_Period_ID:RegionSouth           7.141658e-02  7.974451e-02  8.807245e-02
## Time_Period_ID:RegionWest            5.475924e-02  6.430477e-02  7.385029e-02
## RegionMidwest:I(Time_Period_ID^2)   -9.870165e-04 -7.645227e-04 -5.420290e-04
## RegionNortheast:I(Time_Period_ID^2) -7.629619e-04 -3.292992e-04  1.043635e-04
## RegionSouth:I(Time_Period_ID^2)     -9.583733e-04 -7.482645e-04 -5.381557e-04
## RegionWest:I(Time_Period_ID^2)      -9.404291e-04 -7.087475e-04 -4.770660e-04
##                                          sd_coef
## (Intercept)                         0.0490957400
## StateAlaska                         0.0811131627
## StateArizona                        0.0721492324
## StateArkansas                       0.0538313336
## StateCalifornia                     0.0879657491
## StateColorado                       0.0718326767
## StateConnecticut                    0.1190049832
## StateDelaware                       0.0651198065
## StateFlorida                        0.0549828165
## StateGeorgia                        0.0512051164
## StateHawaii                         0.0742492201
## StateIdaho                          0.0721316506
## StateIllinois                       0.0862293965
## StateIndiana                        0.0744514193
## StateIowa                           0.0731052897
## StateKansas                         0.0673838653
## StateKentucky                       0.0456599777
## StateLouisiana                      0.0510774544
## StateMaine                          0.1222555731
## StateMaryland                       0.1033398255
## StateMassachusetts                  0.1706163396
## StateMichigan                       0.0668473155
## StateMinnesota                      0.0710360630
## StateMississippi                    0.0565540664
## StateMissouri                       0.0658287065
## StateMontana                        0.0765387171
## StateNebraska                       0.0685499310
## StateNevada                         0.0740422480
## StateNew Hampshire                  0.1151186308
## StateNew Jersey                     0.1190308921
## StateNew Mexico                     0.0816284883
## StateNew York                       0.1137212858
## StateNorth Carolina                 0.0443269227
## StateNorth Dakota                   0.0931106490
## StateOhio                           0.0800536790
## StateOklahoma                       0.0560129333
## StateOregon                         0.0718972637
## StatePennsylvania                   0.1036528910
## StateRhode Island                   0.2016437652
## StateSouth Carolina                 0.0504607762
## StateSouth Dakota                   0.0820679025
## StateTennessee                      0.0431007348
## StateTexas                          0.0589842209
## StateUtah                           0.1154679802
## StateVermont                        0.1254101229
## StateVirginia                       0.0484352568
## StateWashington                     0.0725617948
## StateWest Virginia                  0.0716918186
## StateWisconsin                      0.0650177061
## StateWyoming                        0.0865265942
## Naloxone_Pharmacy_Yes_Redefined     0.0315200643
## Naloxone_Pharmacy_No_Redefined      0.0312288969
## Medical_Marijuana_Redefined         0.0360406637
## Recreational_Marijuana_Redefined    0.0385239472
## GSL_Redefined                       0.0267620668
## PDMP_Redefined                      0.0267857235
## Medicaid_Expansion_Redefined        0.0255415494
## pos_0_pd                            0.0404628545
## pos_1_pd                            0.0463346019
## pos_2_pd                            0.0394589631
## pos_3_pd                            0.0406479982
## pos_4_pd                            0.0409948418
## pos_5_pd                            0.0424869393
## pos_6_pd                            0.0453600901
## pos_7_pd                            0.0480339467
## pos_8_pd                            0.0450239010
## pos_9_pd                            0.0493133927
## pos_10_pd                           0.0509245689
## pos_11_pd                           0.0461609957
## pos_12_pd                           0.0501694689
## pos_13_pd                           0.0599765576
## pos_14_pd                           0.0613818152
## pos_15_pd                           0.0563027514
## pos_16_pd                           0.0598170316
## pos_17_pd                           0.0612826655
## pos_18_pd                           0.0612307741
## pos_19_pd                           0.0586989247
## pos_20_pd                           0.0628681264
## pos_21_pd                           0.0684451285
## pos_22_pd                           0.0677658707
## pos_23_pd                           0.0726781649
## pos_24_pd                           0.0775804629
## pos_25_pd                           0.0884034189
## pos_26_pd                           0.0857135144
## pos_27_pd                           0.0911453894
## pos_28_pd                           0.0955009363
## pos_29_pd                           0.1025044471
## pos_30_pd                           0.1060060398
## pos_31_pd                           0.1195927353
## pos_32_pd                           0.1139496052
## pos_33_pd                           0.1425447070
## pos_34_pd                           0.1455064070
## pos_35_pd                           0.1224918631
## pos_36_pd                           0.1233798189
## pos_37_pd                           0.1169364617
## pos_38_pd                           0.0982020642
## pos_39_pd                           0.1305747399
## Time_Period_ID:RegionMidwest        0.0048734711
## Time_Period_ID:RegionNortheast      0.0106267742
## Time_Period_ID:RegionSouth          0.0042489462
## Time_Period_ID:RegionWest           0.0048701665
## RegionMidwest:I(Time_Period_ID^2)   0.0001135172
## RegionNortheast:I(Time_Period_ID^2) 0.0002212565
## RegionSouth:I(Time_Period_ID^2)     0.0001071984
## RegionWest:I(Time_Period_ID^2)      0.0001182049

7.5.2 Plot Results

#plot the coefficients for the periods before and after the intervention with 95% CI
plot_post_tx_log_fixed_lin_time <- sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time %>%
  mutate(term = rownames(sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time)) %>%
  dplyr::select(term, coef_values, lb_coef, ub_coef) %>%
  filter(term %in% c(sapply(0:(max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)), 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) 
colnames(plot_post_tx_log_fixed_lin_time) <- c("term", "estimate", "conf.low", "conf.high")
plot_post_tx_log_fixed_lin_time$num_states <- sapply(plot_post_tx_log_fixed_lin_time$term, function(x){sum(sensitivity_anlys_event_study_data[,x])})

dwplot(plot_post_tx_log_fixed_lin_time, colour = "black",
       vars_order =  c(sapply(((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0), 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45, size = 4)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time Periods", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Post-Intervention Periods") + 
  scale_color_grey() + 
  coord_flip() 

  # geom_vline(aes(xintercept = coef(main_analysis_model_log_fixed_lin_time)["Intervention_Redefined"]), 
  #            linetype = "dashed", color = "red") 
  # geom_text(aes(label = paste("Coefficient Estimate: ", coef(main_analysis_model_log_fixed_lin_time)["Intervention_Redefined"]), y = 12, 
  #           x = coef(main_analysis_model_log_fixed_lin_time)["Intervention_Redefined"] + 0.1), color = "red")
  # geom_text(aes(label = num_states, x = .1, y = 40:1), size = 2)

7.5.3 Attributable Deaths

colnames(sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time) <- c("conf.low", "estimate", "conf.high")
sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time$term <- rownames(sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time)
date_data <- sensitivity_anlys_event_study_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data <- date_data[!duplicated(date_data),]
attr_deaths_est_log_lin_time_diff_post_tx <- attr_death_compute(sensitivity_anlys_event_study_data,
                                                   sensitivity_anlys_post_tx_sd_and_ci_log_fixed_lin_time, 
                                                   lin_model = FALSE )
attr_deaths_est_log_lin_time_diff_post_tx <- merge(attr_deaths_est_log_lin_time_diff_post_tx, date_data, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_diff_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Linear and Quad. Time Effects, 
       Log Probability of Drug Overdose Death, Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

7.5.4 Bootstrapping: Coefficient and Attributable Death Estimate

bootstrap_lin_eff_log_outcome_post_tx<- boostrap_state_time(sensitivity_anlys_event_study_data,
                                                     sensitivity_anlys_post_tx_model_log_fixed_lin_time,
                                                     lin_model = FALSE)

reduced_coef_outcome_lin_eff_log_outcome_post_tx  <- Reduce("cbind", bootstrap_lin_eff_log_outcome_post_tx[[1]])

coef_w_ci_lin_eff_log_outcome_post_tx  <- cbind(t(apply(reduced_coef_outcome_lin_eff_log_outcome_post_tx , 1, quantile, 
                                                                 probs = c(.025, .975), na.rm = TRUE)),
                                             apply(reduced_coef_outcome_lin_eff_log_outcome_post_tx , 1, mean, na.rm = TRUE))
colnames(coef_w_ci_lin_eff_log_outcome_post_tx ) <- c("conf.low", "conf.high", "estimate")
coef_w_ci_lin_eff_log_outcome_post_tx  <- data.frame(coef_w_ci_lin_eff_log_outcome_post_tx )
coef_w_ci_lin_eff_log_outcome_post_tx $term <- rownames(coef_w_ci_lin_eff_log_outcome_post_tx )

dwplot(coef_w_ci_lin_eff_log_outcome_post_tx, colour = "black",
       vars_order =  c(sapply((max(merged_main_time_data_int$Time_Period_ID) - 
                              min(merged_main_time_data_int$intervention_time_id, na.rm = TRUE)):0, 
                                   function(x){paste("pos_", x, "_pd", sep = "")}))) +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"), 
        axis.text.x = element_text(angle = 45)) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Time", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Post-Intervention Periods, Using Bootstrap") + 
  scale_color_grey() + 
  coord_flip() 

list_attr_deaths_lin_eff_post_tx <- lapply(bootstrap_lin_eff_log_outcome_post_tx[[2]], function(x){x$attr_deaths})
reduced_attr_deaths_lin_eff_post_tx <- Reduce("cbind", list_attr_deaths_lin_eff_post_tx)
attr_deaths_lin_eff_post_tx <- cbind(t(apply(reduced_attr_deaths_lin_eff_post_tx, 1, quantile, probs = c(.025, .975), na.rm = TRUE)), 
                                               apply(reduced_attr_deaths_lin_eff_post_tx, 1, mean, na.rm = TRUE))
colnames(attr_deaths_lin_eff_post_tx) <- c("conf.low", "conf.high", "estimate")
attr_deaths_lin_eff_post_tx <- data.frame(attr_deaths_lin_eff_post_tx)
attr_deaths_lin_eff_post_tx$Time_Period_Start <- unique(main_analysis_data$Time_Period_Start)

ggplot(attr_deaths_lin_eff_post_tx, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = estimate, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, Estimated Using Bootstrap",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

8 OLS Model Two Year With Linear Fixed Time Effects Interacted with Region With Log Proportion

8.1 Create Two Year Data

#create a plot for each state to see how many prosecution media alerts there are per 6 month period
#read in the prosecution media alert data
prosecution_data<-read.csv("./Data/dih_prosecutions_9_6_21.csv")

#data cleaning
prosecution_data<-prosecution_data %>% 
  mutate(Date = as.Date(Date.charged, "%m/%d/%Y")) %>%
  mutate(State = ifelse(State.Filed == "pennsylvania", "Pennsylvania", State.Filed),
         State = ifelse(State.Filed == "Virginia ", "Virginia", State)) %>%
  filter(!is.na(Date), State.Filed != "No Info", State.Filed != "No info", State.Filed != "No Info ",
         State != "")

#clean up the data by looking at the link to the article
prosecution_data$Date[prosecution_data$Date == "2026-08-01"] <- as.Date("2016-02-15", "%Y-%m-%d")

#change the states into Character instead of factor
prosecution_data$State<-as.character(prosecution_data$State)
#see how many prosecution data points there are for each state
table(prosecution_data$State)
## 
##        Alabama         Alaska        Arizona       Arkansas     California 
##             12              8              9              4             76 
##       Colorado    Connecticut       Delaware        Florida        Georgia 
##             32             47              3            138             29 
##          Idaho       Illinois        Indiana           Iowa         Kansas 
##              9            342             55             31              9 
##       Kentucky      Louisiana          Maine       Maryland  Massachusetts 
##             43             65             17             63             34 
##       Michigan      Minnesota    Mississippi       Missouri        Montana 
##            116            140              1             45             11 
##       Nebraska         Nevada  New Hampshire     New Jersey     New Mexico 
##              1             13             42            137              4 
##       New York North Carolina   North Dakota           Ohio       Oklahoma 
##            110            124             53            404             41 
##         Oregon   Pennsylvania   Rhode Island South Carolina   South Dakota 
##             19            726              2             12             13 
##      Tennessee          Texas           Utah        Vermont       Virginia 
##             94             44             21             13             63 
##     Washington  West Virginia      Wisconsin        Wyoming 
##             78             33            381             19
#there are some repeated cases depending on victim
prosecution_data_unique <- prosecution_data %>%
  group_by(State) %>%
  distinct(Accused.Name, Date, .keep_all = T)
table(prosecution_data_unique$State)
## 
##        Alabama         Alaska        Arizona       Arkansas     California 
##             12              8              9              4             72 
##       Colorado    Connecticut       Delaware        Florida        Georgia 
##             30             46              3            134             26 
##          Idaho       Illinois        Indiana           Iowa         Kansas 
##              9            336             53             31              9 
##       Kentucky      Louisiana          Maine       Maryland  Massachusetts 
##             43             65             17             62             34 
##       Michigan      Minnesota    Mississippi       Missouri        Montana 
##            114            140              1             44             10 
##       Nebraska         Nevada  New Hampshire     New Jersey     New Mexico 
##              1             13             42            131              4 
##       New York North Carolina   North Dakota           Ohio       Oklahoma 
##            105            121             40            395             34 
##         Oregon   Pennsylvania   Rhode Island South Carolina   South Dakota 
##             19            718              2             12             13 
##      Tennessee          Texas           Utah        Vermont       Virginia 
##             94             43             21             13             63 
##     Washington  West Virginia      Wisconsin        Wyoming 
##             75             33            373             19
#change date charged into Date object
prosecution_data_unique$Date<-mdy(prosecution_data_unique$Date.charged)

#group the data into six month periods
prosecution_data_unique<-prosecution_data_unique %>% 
  mutate(six_month_pd = lubridate::floor_date(Date , "6 months" ))

#count the number of prosecution media alerts in each six month period
#we also get the first and last date of prosecution in time period
prosecution_data_by_six_month_pd <- prosecution_data_unique %>%
  filter(year(six_month_pd)>1999 & year(six_month_pd)<2020) %>%
  group_by(State, six_month_pd) %>%
  summarise(first_date_in_pd = min(Date), last_date_in_pd = max(Date))

#create the data set used for this sensitivity analysis
#first, we merge the grouped prosecution data set with the main data set by state and time period
sensitivity_anlys_redefine_int_data<-merge(main_analysis_data,
                                           prosecution_data_by_six_month_pd,
                                           by.x = c("State", "Time_Period_Start"),
                                           by.y = c("State", "six_month_pd"), all = TRUE)

#create a intervention 2 year effect variable by initializing it to be all 0
sensitivity_anlys_redefine_int_data<-sensitivity_anlys_redefine_int_data %>%
  group_by(State) %>%
  mutate(int_2_yr_effect = 0)

#change the date into a date object
sensitivity_anlys_redefine_int_data$Time_Period_Start<-as.Date(sensitivity_anlys_redefine_int_data$Time_Period_Start)
sensitivity_anlys_redefine_int_data$Time_Period_End<-as.Date(sensitivity_anlys_redefine_int_data$Time_Period_End)

#we need to impute the newly defined intervention variable depending on the case
#by examining each row of the data set
for(state in unique(sensitivity_anlys_redefine_int_data$State)){
  #first, subset the data set into state_data which only contains the data for the state
  state_index<-which(sensitivity_anlys_redefine_int_data$State == state)
  state_data<-sensitivity_anlys_redefine_int_data[state_index,]

  #note that the first four rows of the 2 year effect intervention variable are the same as the
  #first four rows of the original intervention variable
  state_data$int_2_yr_effect[1:4]<-state_data$Intervention_Redefined[1:4]

  for(i in 5:nrow(state_data)){
    #next, we deal with the rows where there was at least one prosecution in the last 3 six month periods
    #These rows will be imputed with a 1
    if((!is.na(state_data$first_date_in_pd[i - 1]) |
        !is.na(state_data$first_date_in_pd[i - 2]) |
        !is.na(state_data$first_date_in_pd[i - 3]))){

      state_data$int_2_yr_effect[i]<-1

    }else{
      #next, we account for the rows with the fractions:
      # 1) an intervention occurs in row i without an intervention 2 years ago
      # 2) row i contains the lasting effects of an intervention that occurred 2 years ago
      # 3) row i contains effects from both a new intervention starting in row i and lasting
      # effects from 2 years ago

      #To compute the fraction, we add the number of days that are affected by an intervention
      #(from both the current prosecution and previous prosecution) and then divide by the total
      #number of days in the period:

      total_len_of_pd<-as.numeric(state_data$Time_Period_End[i] - state_data$Time_Period_Start[i])

      #If there is no prosecution two years ago, i.e. in period i-4, then the last_date is the first
      #date in period i. We subtract the last_date by the first date in the period, so we will get
      #a 0 for the number of days that are affected by a prosecution from period i-4. Otherwise,
      #the last_date is the last date of prosecution from period i-4, plus 2 years.
      len_of_past_effect <- ifelse(!is.na(state_data$first_date_in_pd[i - 4]),
                                   (state_data$last_date_in_pd[i - 4] + years(2)) - state_data$Time_Period_Start[i],
                                   0)

      #If there is no prosecution in the period i, then the start_date is the last date in the period i.
      #We subtract start_date from the last date in period i, so we will get a 0 for the number
      #of days that are affected by a prosecution in period i. Otherwise, the start_date is the
      #first date of a prosecution in period i.
      len_of_current_effect <- ifelse(!is.na(state_data$first_date_in_pd[i]),
                                      as.numeric(state_data$Time_Period_End[i] - state_data$first_date_in_pd[i]),
                                      0)

      state_data$int_2_yr_effect[i]<-(len_of_past_effect + len_of_current_effect)/total_len_of_pd
    }
  }

  #for the case where the int_2_yr_effect is greater than 1 (could result when we add the effects of
  #previous intervention and the current intervention), we just impute a 1 instead
  state_data$int_2_yr_effect[state_data$int_2_yr_effect>1]<-1

  #lastly, we store the int_2_yr_effect variable into the sensitivity analysis data set
  sensitivity_anlys_redefine_int_data$int_2_yr_effect[state_index]<-state_data$int_2_yr_effect
}

#view the data set just to make sure the imputation looks right
# View(sensitivity_anlys_redefine_int_data %>% select(State, Time_Period_Start, Time_Period_End,
#                                                     Intervention_Redefined, first_date_in_pd,
#                                                     last_date_in_pd,
#                                                     int_2_yr_effect))


sensitivity_anlys_redefine_int_data <- sensitivity_anlys_redefine_int_data %>%
  group_by(Time_Period_Start) %>%
  mutate(num_states_w_intervention_2_yr_effect = sum(int_2_yr_effect))

8.2 Constant Effect Model

#compute the proportion of people who died from drug overdose
sensitivity_anlys_redefine_int_data$prop_dead <- sensitivity_anlys_redefine_int_data$imputed_deaths/
  sensitivity_anlys_redefine_int_data$population


#fit an OLS with smoothed time effects
sensitivity_analysis_model_log_fixed_lin_time<-gam(log(prop_dead)~ State +
                                        s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
                                         Naloxone_Pharmacy_Yes_Redefined +
                                         Naloxone_Pharmacy_No_Redefined +
                                         Medical_Marijuana_Redefined +
                                         Recreational_Marijuana_Redefined +
                                         GSL_Redefined +
                                         PDMP_Redefined +
                                         Medicaid_Expansion_Redefined +
                                         int_2_yr_effect ,
                                       data = sensitivity_anlys_redefine_int_data)

summary(sensitivity_analysis_model_log_fixed_lin_time)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + Naloxone_Pharmacy_No_Redefined + 
##     Medical_Marijuana_Redefined + Recreational_Marijuana_Redefined + 
##     GSL_Redefined + PDMP_Redefined + Medicaid_Expansion_Redefined + 
##     int_2_yr_effect
## 
## Parametric coefficients:
##                                   Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.732282   0.051886 -187.572  < 2e-16 ***
## StateAlaska                       0.176722   0.074100    2.385 0.017179 *  
## StateArizona                      0.297802   0.067750    4.396 1.17e-05 ***
## StateArkansas                    -0.477487   0.067136   -7.112 1.61e-12 ***
## StateCalifornia                  -0.165868   0.074090   -2.239 0.025286 *  
## StateColorado                     0.041510   0.074070    0.560 0.575264    
## StateConnecticut                  0.229267   0.070940    3.232 0.001251 ** 
## StateDelaware                     0.231337   0.067625    3.421 0.000637 ***
## StateFlorida                      0.293030   0.066878    4.382 1.24e-05 ***
## StateGeorgia                     -0.011678   0.066057   -0.177 0.859698    
## StateHawaii                      -0.357184   0.072354   -4.937 8.64e-07 ***
## StateIdaho                       -0.109878   0.066484   -1.653 0.098555 .  
## StateIllinois                     0.149450   0.067847    2.203 0.027732 *  
## StateIndiana                      0.085052   0.066309    1.283 0.199766    
## StateIowa                        -0.682383   0.066539  -10.255  < 2e-16 ***
## StateKansas                      -0.227955   0.066228   -3.442 0.000590 ***
## StateKentucky                     0.708997   0.066651   10.638  < 2e-16 ***
## StateLouisiana                    0.332686   0.065948    5.045 4.97e-07 ***
## StateMaine                        0.090388   0.073887    1.223 0.221354    
## StateMaryland                    -1.524942   0.067627  -22.549  < 2e-16 ***
## StateMassachusetts               -0.082850   0.067360   -1.230 0.218863    
## StateMichigan                     0.004729   0.068479    0.069 0.944949    
## StateMinnesota                   -0.645614   0.069958   -9.229  < 2e-16 ***
## StateMississippi                 -0.035631   0.066179   -0.538 0.590357    
## StateMissouri                     0.161791   0.068241    2.371 0.017845 *  
## StateMontana                     -0.446753   0.070067   -6.376 2.27e-10 ***
## StateNebraska                    -0.862333   0.067404  -12.794  < 2e-16 ***
## StateNevada                       0.457017   0.071694    6.375 2.29e-10 ***
## StateNew Hampshire                0.150070   0.067115    2.236 0.025466 *  
## StateNew Jersey                   0.071248   0.067671    1.053 0.292541    
## StateNew Mexico                   0.651578   0.073136    8.909  < 2e-16 ***
## StateNew York                    -0.140484   0.068388   -2.054 0.040091 *  
## StateNorth Carolina               0.227446   0.065851    3.454 0.000564 ***
## StateNorth Dakota                -1.129127   0.066271  -17.038  < 2e-16 ***
## StateOhio                         0.437070   0.067004    6.523 8.79e-11 ***
## StateOklahoma                     0.470225   0.066373    7.085 1.95e-12 ***
## StateOregon                      -0.269650   0.073841   -3.652 0.000267 ***
## StatePennsylvania                 0.546397   0.066969    8.159 6.04e-16 ***
## StateRhode Island                -0.266728   0.069446   -3.841 0.000127 ***
## StateSouth Carolina               0.222407   0.066049    3.367 0.000774 ***
## StateSouth Dakota                -1.011264   0.066381  -15.234  < 2e-16 ***
## StateTennessee                    0.470629   0.065763    7.156 1.17e-12 ***
## StateTexas                       -0.029320   0.066410   -0.442 0.658899    
## StateUtah                        -0.098664   0.065991   -1.495 0.135049    
## StateVermont                     -0.166096   0.069776   -2.380 0.017390 *  
## StateVirginia                    -0.040830   0.065975   -0.619 0.536080    
## StateWashington                   0.044179   0.075280    0.587 0.557361    
## StateWest Virginia                0.799647   0.066602   12.006  < 2e-16 ***
## StateWisconsin                   -0.002745   0.066233   -0.041 0.966943    
## StateWyoming                     -0.017430   0.066089   -0.264 0.792015    
## Naloxone_Pharmacy_Yes_Redefined  -0.082459   0.042748   -1.929 0.053882 .  
## Naloxone_Pharmacy_No_Redefined   -0.005978   0.038644   -0.155 0.877073    
## Medical_Marijuana_Redefined       0.193591   0.030661    6.314 3.37e-10 ***
## Recreational_Marijuana_Redefined -0.114103   0.048891   -2.334 0.019708 *  
## GSL_Redefined                     0.057993   0.031575    1.837 0.066410 .  
## PDMP_Redefined                   -0.154318   0.024687   -6.251 5.01e-10 ***
## Medicaid_Expansion_Redefined      0.090679   0.030122    3.010 0.002643 ** 
## int_2_yr_effect                   0.012986   0.021508    0.604 0.546043    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.777  5.834 145.24  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.468  8.918  84.05  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.291  7.433 109.20  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      3.421  4.263  86.60  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.84   Deviance explained = 84.6%
## GCV = 0.08963  Scale est. = 0.086002  n = 2000
#examine fitted values
summary(fitted(sensitivity_analysis_model_log_fixed_lin_time))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
## -12.327 -10.207  -9.740  -9.796  -9.343  -8.133
hist(fitted(sensitivity_analysis_model_log_fixed_lin_time))

par(mfrow = c(2,2))
plot(sensitivity_analysis_model_log_fixed_lin_time)

8.2.1 Coefficients and 95% CI

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_2_yr <- model.matrix(sensitivity_analysis_model_log_fixed_lin_time)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_sens_anlys <- coef(sensitivity_analysis_model_log_fixed_lin_time)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time <- compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time_2_yr,
                                                                log(sensitivity_anlys_redefine_int_data$prop_dead),
                                             coefficient_values_log_fixed_lin_time_sens_anlys,
                                             p = ncol(full_df_w_basis_functions_log_fixed_lin_time_2_yr) - 50)

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time$term <- rownames(sens_analysis_sd_and_ci_log_fixed_lin_time)


sens_analysis_sd_and_ci_log_fixed_lin_time$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time[51:58,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Effects, 
       2 Year Exposure Intervention") + 
  scale_color_grey()     + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time[51:58,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 8:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time[51:58,], 
            mapping = aes(label = ci_95, x = 0.9, y = 8:1), size = 3) +
  xlim(-.3, 1.1)

8.2.2 Attributable Deaths

date_data_sens_anlys <- sensitivity_anlys_redefine_int_data[, c("Time_Period_ID", "Time_Period_Start")]
date_data_sens_anlys <- date_data_sens_anlys[!duplicated(date_data_sens_anlys),]
attr_deaths_est_log_lin_time <- attr_death_compute(sensitivity_anlys_redefine_int_data,
                                                   sens_analysis_sd_and_ci_log_fixed_lin_time, 
                                                   lin_model = TRUE, tx_name = "int_2_yr_effect")
attr_deaths_est_log_lin_time_sens_anlys <- merge(attr_deaths_est_log_lin_time, date_data_sens_anlys, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_sens_anlys, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Effect",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

8.3 Create Linear Trend Data

#use this function to compute the cumulative sum, but resets the sum if the variable was 0
compute_cumsum = function(x){
    cs = cumsum(x)
    cs - cummax((x == 0) * cs)
}
sensitivity_anlys_event_study_data_lin_post_tx_2yr <- sensitivity_anlys_redefine_int_data %>%
  arrange(State, Time_Period_ID) %>%
  group_by(State) %>%
  mutate(num_pd_w_tx = compute_cumsum(int_2_yr_effect),
         num_pd_w_naloxone_yes = compute_cumsum(Naloxone_Pharmacy_Yes_Redefined),
         num_pd_w_naloxone_no = compute_cumsum(Naloxone_Pharmacy_No_Redefined),
         num_pd_w_med_marijuana = compute_cumsum(Medical_Marijuana_Redefined),
         num_pd_w_rec_marijuana = compute_cumsum(Recreational_Marijuana_Redefined),
         num_pd_w_gsl = compute_cumsum(GSL_Redefined),
         num_pd_w_pdmp = compute_cumsum(PDMP_Redefined),
         num_pd_w_medicaid = compute_cumsum(Medicaid_Expansion_Redefined),
         lag_num_pd_w_tx = lag(num_pd_w_tx),
         lag_num_pd_w_naloxone_yes = lag(num_pd_w_naloxone_yes),
         lag_num_pd_w_naloxone_no = lag(num_pd_w_naloxone_no),
         lag_num_pd_w_med_marijuana = lag(num_pd_w_med_marijuana),
         lag_num_pd_w_rec_marijuana = lag(num_pd_w_rec_marijuana),
         lag_num_pd_w_gsl = lag(num_pd_w_gsl),
         lag_num_pd_w_pdmp = lag(num_pd_w_pdmp),
         lag_num_pd_w_medicaid = lag(num_pd_w_medicaid)) #lag so that intercept = effect when tx first occurs

#fill in a 0 for the NAs so we keep all the data and at most this will be 0
sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_tx[
  is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_tx)] <- 
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_yes[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_yes)]<-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_no[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_naloxone_no)] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_med_marijuana[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_med_marijuana)] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_rec_marijuana[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_rec_marijuana)] <- 
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_gsl[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_gsl)] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_pdmp[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_pdmp)] <-
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_medicaid[
    is.na(sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_medicaid)] <-0

#for the linear effects, we want the 2 year effect variable to be 0 when treatment is 0
sensitivity_anlys_event_study_data_lin_post_tx_2yr$lag_num_pd_w_tx[
  sensitivity_anlys_event_study_data_lin_post_tx_2yr$int_2_yr_effect == 0] <- 0

8.4 Model

#run the gam model
sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr<-gam(log(prop_dead)~ State +
                                                             s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                             Naloxone_Pharmacy_Yes_Redefined + 
                                                             lag_num_pd_w_naloxone_yes +
                                                             Naloxone_Pharmacy_No_Redefined + 
                                                             lag_num_pd_w_naloxone_no +
                                                             Medical_Marijuana_Redefined + 
                                                             lag_num_pd_w_med_marijuana +
                                                             Recreational_Marijuana_Redefined + 
                                                             lag_num_pd_w_rec_marijuana +
                                                             GSL_Redefined + 
                                                             lag_num_pd_w_gsl +
                                                             PDMP_Redefined + 
                                                             lag_num_pd_w_pdmp +
                                                             Medicaid_Expansion_Redefined +
                                                             lag_num_pd_w_medicaid +
                                                             int_2_yr_effect +
                                                             lag_num_pd_w_tx,
                                                           data = sensitivity_anlys_event_study_data_lin_post_tx_2yr)
summary(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + int_2_yr_effect + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -9.8076233  0.0608659 -161.135  < 2e-16 ***
## StateAlaska                       0.2523817  0.0833006    3.030 0.002480 ** 
## StateArizona                      0.3152088  0.0671827    4.692 2.90e-06 ***
## StateArkansas                    -0.4577377  0.0686862   -6.664 3.47e-11 ***
## StateCalifornia                  -0.0850394  0.0841628   -1.010 0.312425    
## StateColorado                     0.1208318  0.0796243    1.518 0.129300    
## StateConnecticut                  0.3246719  0.0742477    4.373 1.29e-05 ***
## StateDelaware                     0.2708122  0.0685064    3.953 8.00e-05 ***
## StateFlorida                      0.4768304  0.0719049    6.631 4.31e-11 ***
## StateGeorgia                      0.0861630  0.0677328    1.272 0.203492    
## StateHawaii                      -0.3353486  0.0849154   -3.949 8.13e-05 ***
## StateIdaho                       -0.1920278  0.0696288   -2.758 0.005873 ** 
## StateIllinois                     0.2921292  0.0753486    3.877 0.000109 ***
## StateIndiana                      0.0067938  0.0692899    0.098 0.921904    
## StateIowa                        -0.6544592  0.0655296   -9.987  < 2e-16 ***
## StateKansas                      -0.2150774  0.0656836   -3.274 0.001078 ** 
## StateKentucky                     0.6473263  0.0695627    9.306  < 2e-16 ***
## StateLouisiana                    0.3724967  0.0647567    5.752 1.02e-08 ***
## StateMaine                        0.2243701  0.0836097    2.684 0.007348 ** 
## StateMaryland                    -1.4894682  0.0703307  -21.178  < 2e-16 ***
## StateMassachusetts               -0.1967530  0.0704965   -2.791 0.005307 ** 
## StateMichigan                     0.0210692  0.0735977    0.286 0.774698    
## StateMinnesota                   -0.5261783  0.0721977   -7.288 4.58e-13 ***
## StateMississippi                 -0.0612346  0.0646141   -0.948 0.343403    
## StateMissouri                     0.2944134  0.0748907    3.931 8.75e-05 ***
## StateMontana                     -0.2935909  0.0749570   -3.917 9.29e-05 ***
## StateNebraska                    -0.8364703  0.0687058  -12.175  < 2e-16 ***
## StateNevada                       0.4626870  0.0811187    5.704 1.35e-08 ***
## StateNew Hampshire                0.2411839  0.0694031    3.475 0.000522 ***
## StateNew Jersey                   0.1193215  0.0673596    1.771 0.076652 .  
## StateNew Mexico                   0.6952851  0.0808901    8.595  < 2e-16 ***
## StateNew York                    -0.2214778  0.0717181   -3.088 0.002043 ** 
## StateNorth Carolina               0.2689043  0.0646416    4.160 3.32e-05 ***
## StateNorth Dakota                -1.1441378  0.0647965  -17.657  < 2e-16 ***
## StateOhio                         0.6014005  0.0706398    8.514  < 2e-16 ***
## StateOklahoma                     0.4243367  0.0694560    6.109 1.21e-09 ***
## StateOregon                      -0.0376676  0.0824836   -0.457 0.647962    
## StatePennsylvania                 0.6313240  0.0741509    8.514  < 2e-16 ***
## StateRhode Island                -0.3052086  0.0748633   -4.077 4.75e-05 ***
## StateSouth Carolina               0.2159620  0.0645860    3.344 0.000842 ***
## StateSouth Dakota                -0.9789684  0.0669369  -14.625  < 2e-16 ***
## StateTennessee                    0.4412286  0.0649703    6.791 1.48e-11 ***
## StateTexas                       -0.0504875  0.0696987   -0.724 0.468928    
## StateUtah                        -0.1944484  0.0693577   -2.804 0.005105 ** 
## StateVermont                     -0.0852747  0.0714458   -1.194 0.232799    
## StateVirginia                     0.0222502  0.0676259    0.329 0.742177    
## StateWashington                   0.1697629  0.0814168    2.085 0.037192 *  
## StateWest Virginia                0.6838179  0.0698390    9.791  < 2e-16 ***
## StateWisconsin                    0.1456205  0.0690747    2.108 0.035147 *  
## StateWyoming                     -0.0576551  0.0652175   -0.884 0.376784    
## Naloxone_Pharmacy_Yes_Redefined  -0.0597864  0.0426982   -1.400 0.161613    
## lag_num_pd_w_naloxone_yes        -0.0313473  0.0067039   -4.676 3.13e-06 ***
## Naloxone_Pharmacy_No_Redefined    0.0850900  0.0417596    2.038 0.041726 *  
## lag_num_pd_w_naloxone_no         -0.0189385  0.0040962   -4.623 4.03e-06 ***
## Medical_Marijuana_Redefined       0.1972907  0.0305893    6.450 1.42e-10 ***
## lag_num_pd_w_med_marijuana       -0.0085599  0.0022109   -3.872 0.000112 ***
## Recreational_Marijuana_Redefined -0.0341100  0.0623464   -0.547 0.584371    
## lag_num_pd_w_rec_marijuana        0.0006513  0.0111255    0.059 0.953324    
## GSL_Redefined                     0.0903340  0.0335141    2.695 0.007092 ** 
## lag_num_pd_w_gsl                  0.0112105  0.0041076    2.729 0.006407 ** 
## PDMP_Redefined                   -0.1261603  0.0275392   -4.581 4.92e-06 ***
## lag_num_pd_w_pdmp                 0.0079005  0.0023995    3.293 0.001011 ** 
## Medicaid_Expansion_Redefined      0.0842950  0.0340798    2.473 0.013468 *  
## lag_num_pd_w_medicaid             0.0148504  0.0051077    2.907 0.003686 ** 
## int_2_yr_effect                   0.0379656  0.0211853    1.792 0.073279 .  
## lag_num_pd_w_tx                  -0.0124931  0.0018584   -6.723 2.35e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df     F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   4.151  5.132 90.19  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 8.530  8.936 46.10  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     6.359  7.507 49.73  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      4.177  5.182 32.85  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =  0.848   Deviance explained = 85.5%
## GCV = 0.085463  Scale est. = 0.081651  n = 2000
plot(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr, pages = 1)

8.4.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr <- model.matrix(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx_2yr <- coef(sensitivity_anlys_lin_post_tx_model_log_smoothed_time_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx <- compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr,
                                                                log(sensitivity_anlys_event_study_data_lin_post_tx_2yr$prop_dead),
                                             coefficient_values_log_fixed_lin_time_lin_post_tx_2yr,
                                             p = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_2_yr) - 50)
# format(round(main_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx,4)

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term <- rownames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx)




sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention") + 
  scale_color_grey()      + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.3, 1.1)

8.4.2 Plot Effects

plot_data <- data.frame(time_after_tx = 0:38,
                        coef = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$estimate[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.low = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.low[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38),
                        conf.high = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$conf.high[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx$term == "lag_num_pd_w_tx"]*c(0:38))

ggplot(plot_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

8.4.3 Attributable Deaths

date_data_2yr <- sensitivity_anlys_event_study_data_lin_post_tx_2yr[, c("Time_Period_ID", "Time_Period_Start")]
date_data_2yr <- date_data_2yr[!duplicated(date_data_2yr),]
attr_deaths_est_log_lin_time_lin_post_tx_2yr <- attr_death_compute(sensitivity_anlys_event_study_data_lin_post_tx_2yr,
                                                   sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx, 
                                                   lin_model = TRUE, tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"))
attr_deaths_est_log_lin_time_lin_post_tx_2yr <- merge(attr_deaths_est_log_lin_time_lin_post_tx_2yr, date_data_2yr, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_lin_post_tx_2yr, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))

8.5 Linear Policy Measures Post Tx Subset

sens_data_subset <- sensitivity_anlys_event_study_data_lin_post_tx_2yr %>%
  filter(Time_Period_ID <= 30)
#run the gam model
sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr<-gam(log(prop_dead)~ State +
                                                             s(Time_Period_ID, bs = 'cr', by = as.factor(Region))  +
                                                             Naloxone_Pharmacy_Yes_Redefined + 
                                                             lag_num_pd_w_naloxone_yes +
                                                             Naloxone_Pharmacy_No_Redefined + 
                                                             lag_num_pd_w_naloxone_no +
                                                             Medical_Marijuana_Redefined + 
                                                             lag_num_pd_w_med_marijuana +
                                                             Recreational_Marijuana_Redefined + 
                                                             lag_num_pd_w_rec_marijuana +
                                                             GSL_Redefined + 
                                                             lag_num_pd_w_gsl +
                                                             PDMP_Redefined + 
                                                             lag_num_pd_w_pdmp +
                                                             Medicaid_Expansion_Redefined +
                                                             lag_num_pd_w_medicaid +
                                                             int_2_yr_effect +
                                                             lag_num_pd_w_tx,
                                                           data = sens_data_subset)
summary(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
## 
## Family: gaussian 
## Link function: identity 
## 
## Formula:
## log(prop_dead) ~ State + s(Time_Period_ID, bs = "cr", by = as.factor(Region)) + 
##     Naloxone_Pharmacy_Yes_Redefined + lag_num_pd_w_naloxone_yes + 
##     Naloxone_Pharmacy_No_Redefined + lag_num_pd_w_naloxone_no + 
##     Medical_Marijuana_Redefined + lag_num_pd_w_med_marijuana + 
##     Recreational_Marijuana_Redefined + lag_num_pd_w_rec_marijuana + 
##     GSL_Redefined + lag_num_pd_w_gsl + PDMP_Redefined + lag_num_pd_w_pdmp + 
##     Medicaid_Expansion_Redefined + lag_num_pd_w_medicaid + int_2_yr_effect + 
##     lag_num_pd_w_tx
## 
## Parametric coefficients:
##                                    Estimate Std. Error  t value Pr(>|t|)    
## (Intercept)                      -10.028666   0.060388 -166.071  < 2e-16 ***
## StateAlaska                        0.434385   0.095279    4.559 5.58e-06 ***
## StateArizona                       0.440961   0.075377    5.850 6.09e-09 ***
## StateArkansas                     -0.392536   0.076986   -5.099 3.88e-07 ***
## StateCalifornia                    0.089884   0.098429    0.913 0.361299    
## StateColorado                      0.263986   0.091134    2.897 0.003829 ** 
## StateConnecticut                   0.519101   0.087323    5.945 3.48e-09 ***
## StateDelaware                      0.216244   0.077328    2.796 0.005236 ** 
## StateFlorida                       0.615345   0.083473    7.372 2.85e-13 ***
## StateGeorgia                       0.140347   0.077155    1.819 0.069117 .  
## StateHawaii                       -0.288143   0.096437   -2.988 0.002857 ** 
## StateIdaho                        -0.222227   0.080103   -2.774 0.005605 ** 
## StateIllinois                      0.374002   0.086816    4.308 1.76e-05 ***
## StateIndiana                      -0.118838   0.079615   -1.493 0.135748    
## StateIowa                         -0.649582   0.074158   -8.759  < 2e-16 ***
## StateKansas                       -0.134710   0.074963   -1.797 0.072544 .  
## StateKentucky                      0.617360   0.079783    7.738 1.91e-14 ***
## StateLouisiana                     0.406523   0.074416    5.463 5.53e-08 ***
## StateMaine                         0.161182   0.093732    1.720 0.085722 .  
## StateMaryland                     -1.713490   0.079851  -21.459  < 2e-16 ***
## StateMassachusetts                -0.398089   0.080225   -4.962 7.81e-07 ***
## StateMichigan                     -0.028834   0.082170   -0.351 0.725709    
## StateMinnesota                    -0.492719   0.082215   -5.993 2.61e-09 ***
## StateMississippi                   0.025279   0.074214    0.341 0.733434    
## StateMissouri                      0.298344   0.079695    3.744 0.000189 ***
## StateMontana                      -0.157199   0.083739   -1.877 0.060688 .  
## StateNebraska                     -0.773352   0.076978  -10.046  < 2e-16 ***
## StateNevada                        0.621854   0.091903    6.766 1.93e-11 ***
## StateNew Hampshire                 0.163385   0.077856    2.099 0.036034 *  
## StateNew Jersey                    0.087638   0.076388    1.147 0.251459    
## StateNew Mexico                    1.128853   0.099257   11.373  < 2e-16 ***
## StateNew York                     -0.146980   0.083825   -1.753 0.079749 .  
## StateNorth Carolina                0.293372   0.074800    3.922 9.20e-05 ***
## StateNorth Dakota                 -1.221095   0.074068  -16.486  < 2e-16 ***
## StateOhio                          0.600554   0.081991    7.325 4.00e-13 ***
## StateOklahoma                      0.443081   0.079630    5.564 3.14e-08 ***
## StateOregon                        0.127537   0.095244    1.339 0.180768    
## StatePennsylvania                  0.635587   0.085615    7.424 1.95e-13 ***
## StateRhode Island                 -0.573207   0.084653   -6.771 1.86e-11 ***
## StateSouth Carolina                0.230078   0.074391    3.093 0.002021 ** 
## StateSouth Dakota                 -0.996396   0.076462  -13.031  < 2e-16 ***
## StateTennessee                     0.397908   0.074918    5.311 1.26e-07 ***
## StateTexas                        -0.015681   0.079715   -0.197 0.844076    
## StateUtah                         -0.303867   0.079644   -3.815 0.000142 ***
## StateVermont                      -0.074113   0.081460   -0.910 0.363079    
## StateVirginia                     -0.010813   0.077568   -0.139 0.889150    
## StateWashington                    0.420578   0.093845    4.482 8.00e-06 ***
## StateWest Virginia                 0.623437   0.079871    7.806 1.14e-14 ***
## StateWisconsin                     0.156826   0.078981    1.986 0.047269 *  
## StateWyoming                      -0.012870   0.074664   -0.172 0.863168    
## Naloxone_Pharmacy_Yes_Redefined   -0.206890   0.076017   -2.722 0.006575 ** 
## lag_num_pd_w_naloxone_yes         -0.060614   0.023659   -2.562 0.010511 *  
## Naloxone_Pharmacy_No_Redefined     0.090501   0.053389    1.695 0.090272 .  
## lag_num_pd_w_naloxone_no          -0.041683   0.005547   -7.514 1.01e-13 ***
## Medical_Marijuana_Redefined        0.221983   0.039558    5.612 2.41e-08 ***
## lag_num_pd_w_med_marijuana        -0.016007   0.003079   -5.199 2.29e-07 ***
## Recreational_Marijuana_Redefined  -0.188734   0.191257   -0.987 0.323905    
## lag_num_pd_w_rec_marijuana         0.006791   0.091601    0.074 0.940911    
## GSL_Redefined                      0.138885   0.055509    2.502 0.012460 *  
## lag_num_pd_w_gsl                   0.019149   0.010334    1.853 0.064088 .  
## PDMP_Redefined                    -0.112676   0.029720   -3.791 0.000156 ***
## lag_num_pd_w_pdmp                  0.014078   0.002763    5.095 3.95e-07 ***
## Medicaid_Expansion_Redefined       0.076342   0.051119    1.493 0.135552    
## lag_num_pd_w_medicaid              0.015014   0.014957    1.004 0.315631    
## int_2_yr_effect                    0.031860   0.024712    1.289 0.197526    
## lag_num_pd_w_tx                   -0.020304   0.002961   -6.856 1.05e-11 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Approximate significance of smooth terms:
##                                                edf Ref.df      F p-value    
## s(Time_Period_ID):as.factor(Region)Midwest   2.694  3.363 115.81  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)Northeast 7.589  8.491  37.88  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)South     3.115  3.874  76.00  <2e-16 ***
## s(Time_Period_ID):as.factor(Region)West      2.787  3.483  44.67  <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## R-sq.(adj) =   0.83   Deviance explained = 83.9%
## GCV = 0.086342  Scale est. = 0.081611  n = 1500

8.5.1 Sandwich Estimator

#compute the full dataset including basis functions
full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr <-
  model.matrix(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)

#estimate the 95% CI and SD
coefficient_values_log_fixed_lin_time_lin_post_tx_subset_2yr <- coef(sensitivity_anlys_lin_post_tx_model_linear_time_subset_2yr)
#type = "response" to get the estimated probabilities
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset <-
  compute_sd_and_CI(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr,
                    log(sens_data_subset$prop_dead),
                    coefficient_values_log_fixed_lin_time_lin_post_tx_subset_2yr,
                    p = ncol(full_df_w_basis_functions_log_fixed_lin_time_lin_post_tx_subset_2yr) - 50)
# format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset,4)

colnames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset) <- c("conf.low", "estimate", "conf.high", "sd")
sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term <-
  rownames(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset)


sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$ci_95 <- 
  paste("95% CI = (", format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low, 3), nsmall = 3), ", ", 
        format(round(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high, 3), nsmall = 3), ")", sep = "")
                                                                         
dwplot(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], colour = "black") +  
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  geom_vline(aes(xintercept = 0), linetype = "dashed") +
  labs(y = "Term", x = "Coefficients and 95% Confidence Intervals", 
       title = "Coefficient of Analysis With Smoothed Time Effects, 
       2 Year Linear Intervention, Subset Data") + 
  scale_color_grey()  + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = format(round(estimate, 3), nsmall = 3), x = 0.55, y = 16:1), size = 3) + 
  geom_text(sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset[51:66,], 
            mapping = aes(label = ci_95, x = 0.9, y = 16:1), size = 3) +
  xlim(-.5, 1.1)

8.5.2 Plot Effects

plot_data_subset_data <- data.frame(time_after_tx = 0:28,
                        coef = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$estimate[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28),
                        conf.low = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.low[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28),
                        conf.high = sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "int_2_yr_effect"] + 
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$conf.high[
                          sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset$term == "lag_num_pd_w_tx"]*c(0:28))

ggplot(plot_data_subset_data, aes(x = time_after_tx)) + 
  geom_line(aes(y = coef, linetype = "Estimated Effect")) + 
  geom_line(aes(y = conf.low, linetype = "95% CI")) + 
  geom_line(aes(y = conf.high, linetype = "95% CI")) + 
  scale_linetype_manual(values = c("dashed", "solid")) + 
  labs(linetype = "",
       x = "Number of Periods After Treatment",
       y = "Estimated Effect",
       title = "Estimated Effect of DIH Prosecutions Reported in the Media on 
       Drug OD Deaths by Number of Periods After Exposure, 
       Assuming Two Year Effect, Without Last 5 Years") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black"),
        legend.position = "bottom") + 
  geom_hline(aes(yintercept = 0), color = "grey", linetype = "dotted")

8.5.3 Attributable Deaths

date_data_subset_2yr <- sens_data_subset[, c("Time_Period_ID", "Time_Period_Start")]
date_data_subset_2yr <- date_data_subset_2yr[!duplicated(date_data_subset_2yr),]
attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr <- attr_death_compute(sens_data_subset,
                                                   sens_analysis_sd_and_ci_log_fixed_lin_time_lin_post_tx_subset, 
                                                   lin_model = TRUE, tx_name = c("int_2_yr_effect", "lag_num_pd_w_tx"))
attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr <- merge(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr, date_data_subset_2yr, 
                                                    by.x = "Time_Period", by.y = "Time_Period_ID")

ggplot(attr_deaths_est_log_lin_time_lin_post_tx_subset_2yr, aes(x = Time_Period_Start)) + 
  # geom_point(aes(y = attr_deaths)) + 
  geom_line(aes(y = attr_deaths, linetype = "Estimate")) + 
  # geom_point(aes(y = attr_deaths_lb)) + 
  geom_line(aes(y = attr_deaths_lb, linetype = "95% CI")) + 
  # geom_point(aes(y = attr_deaths_ub)) + 
  geom_line(aes(y = attr_deaths_ub, linetype = "95% CI")) + 
  labs(x = "Date", y = "Lives Saved",
       title = "Estimated Number of Lives Saved Using Smoothed Time Effects, 
       Log Probability of Drug Overdose Death, 2 Year Linear Policy Effects",
       linetype = "") + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        panel.background = element_blank(), axis.line = element_line(colour = "black")) + 
  scale_linetype_manual(values = c("dashed", "solid"))